About solution sharing for hidden controls under ListView-Practical Tips

Source: Internet
Author: User
Tags eval
This is the code in the ListView template, posted to facilitate understanding:
Programme I:
Copy Code code as follows:

<span style= "font-size:15px" > Just want to hide DeleteButton and Editbutton 2 buttons </SPAN>

Copy Code code as follows:

<ItemTemplate>
<tr>
<td>
<asp:button id= "DeleteButton" runat= "server" commandname= "delete" text= "Remove"/>
<asp:button id= "Editbutton" runat= "Server"
Commandname= "Edit" text= "editor"/>
</td>
&LT;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>

At first, I thought it was simple, and I wrote down the following code:
Copy Code code 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")
{
Editbutton.visible = false;
Deletebutton.visible = false;
}
}

However, when running, it begins to complain that the object reference is set to an instance of the object. That is, I didn't find this control, I was very strange, feel very unreasonable. It is supposed to be OK to itemcreated after the initialization of the row, and then to find the control.
This question, thought for a long time, no results, and then I was a classmate (he is good) to start the discussion, just at the beginning he said yes, that is, when the page is running,
To see the source belt, found DeleteButton the control ID changed, and became Listview1_ctrl0_deletebutton this way. For this question,
I have not figured out until now, I do not know how MS can do this out? Server control is not a good rendering to the page, how to change the ID it? No reason. Oh ~ ~
Then, my classmate after debugging, gave a solution is:
Copy Code code 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;
}
}
}

It's OK. is to add a sentence: if (DeleteButton!= null && editbutton!= null) judgment.
Programme II:
Very talented is, my dormitory of a Hebei classmate also gave a solution, but this kind of writing looks a bit incredible, hehe, together to see
It!
Copy Code code as follows:
<asp:button id= "DeleteButton" runat= "visible=" <% #Session ["username"]== "admin"? True:false%> '

Just like this, you can also hide the control. Because the session is a global variable, this value can be taken from the page.

Author: lanny☆ Landong only

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.