Solution sharing for hidden controls under ListView
- 2020-05-17 05:04:23
- OfStack
This is the code in ListView template. Please paste 1 for easy understanding:
Plan 1:
1 at first, thinking it was easy, I wrote the following code:
However, when it is run, it begins to report that the object reference is set to an instance of the object. That is to say, did not find this control, I am very strange, feel very unreasonable. ItemCreated is supposed to look for the control after initializing the line, so that should be fine.
This problem, thought for a long time, no results, and then I began to discuss with a classmate (he is good technology), he said at the beginning, that is, when the page is running, in
When I went to check the source code, I found that the ID of DeleteButton had changed to ListView1_ctrl0_DeleteButton. So for this question,
I haven't figured it out until now. I wonder how MS got this out. Server control is not well rendered to the page, how to change ID? There is no reason to. Ha ha ~ ~
Then, after debugging, my classmate gave a solution as follows:
Just like that. That's one more sentence: if (DeleteButton! = null && EditButton! = null).
Scheme 2:
Very talented is, my dormitory 1 hebei classmate also gave 1 kind of solution, but this kind of writing looks a bit incredible, ha ha, 1 take a look
!!!!
As such, you can also hide the control. Because session is a global variable, you can get this value on the page.
By Lanny ☆ landongcai
Plan 1:
<SPAN style="FONT-SIZE: 15px"> Just want to put the DeleteButton and EditButton 2 Button hide </SPAN>
<ItemTemplate>
<tr>
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text=" delete " />
<asp:Button ID="EditButton" runat="server"
CommandName="Edit" Text=" The editor " />
</td>
<td style="">
<div style="width: 30px;">
<asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("UserName") %>' Width="30px" /></div>
</td>
<td>
<asp:Label ID="AgeLabel" runat="server" Text='<%# Eval("Age") %>' />
</td>
<td>
<asp:Label ID="IdentityCardLabel" runat="server" Text='<%# Eval("IdentityCard") %>' />
</td>
<td>
<asp:Label ID="LoginDateLabel" runat="server" Text='<%# Eval("LoginDate","{0:yyyy-MM-dd}") %>' />
</td>
<td>
<asp:Label ID="LeaveDateLabel" runat="server" Text='<%# Eval("LeaveDate","{0:yyyy-MM-dd}") %>' />
</td>
<td>
<asp:Label ID="PopulationLabel" runat="server" Text='<%# Eval("Population") %>' />
</td>
<td>
<asp:Label ID="HouseIDLabel" runat="server" Text='<%# Eval("HouseID") %>' />
</td>
</tr>
</ItemTemplate>
1 at first, thinking it was easy, I wrote the following code:
protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
{
Button DeleteButton = (Button)ListView1.FindControl("DeleteButton");
Button EditButton = (Button)ListView1.FindControl("EditButton");
string username = Session["username"].ToString();
if (username != "admin")
{
EditButton.Visible = false;
DeleteButton.Visible = false;
}
}
However, when it is run, it begins to report that the object reference is set to an instance of the object. That is to say, did not find this control, I am very strange, feel very unreasonable. ItemCreated is supposed to look for the control after initializing the line, so that should be fine.
This problem, thought for a long time, no results, and then I began to discuss with a classmate (he is good technology), he said at the beginning, that is, when the page is running, in
When I went to check the source code, I found that the ID of DeleteButton had changed to ListView1_ctrl0_DeleteButton. So for this question,
I haven't figured it out until now. I wonder how MS got this out. Server control is not well rendered to the page, how to change ID? There is no reason to. Ha ha ~ ~
Then, after debugging, my classmate gave a solution as follows:
protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
{
Button DeleteButton = (Button)ListView1.FindControl("DeleteButton");
Button EditButton = (Button)ListView1.FindControl("EditButton");
string username = Session["username"].ToString();
if (username != "admin")
{
if (DeleteButton != null && EditButton != null)
{
EditButton.Visible = false;
DeleteButton.Visible = false;
}
}
}
Just like that. That's one more sentence: if (DeleteButton! = null && EditButton! = null).
Scheme 2:
Very talented is, my dormitory 1 hebei classmate also gave 1 kind of solution, but this kind of writing looks a bit incredible, ha ha, 1 take a look
!!!!
<asp:Button ID="DeleteButton" runat="server" Visible='<%#Session["username"]=="admin"?true:false %>'
As such, you can also hide the control. Because session is a global variable, you can get this value on the page.
By Lanny ☆ landongcai