這是ListView模板里的代碼,貼一下,方便理解:
方案一:
復(fù)制代碼 代碼如下:
SPAN style="FONT-SIZE: 15px">就是想把DeleteButton 和EditButton 2個(gè)按鈕隱藏/SPAN>
復(fù)制代碼 代碼如下:
ItemTemplate>
tr>
td>
asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="刪除" />
asp:Button ID="EditButton" runat="server"
CommandName="Edit" Text="編輯" />
/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>
一開(kāi)始,覺(jué)得這挺簡(jiǎn)單,就寫(xiě)下了下面的代碼:
復(fù)制代碼 代碼如下:
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;
}
}
可是,當(dāng)運(yùn)行的時(shí)候,就開(kāi)始報(bào)錯(cuò)說(shuō),將對(duì)象引用設(shè)置到對(duì)象的實(shí)例。就是說(shuō)沒(méi)有找到這個(gè)控件,我就很奇怪了,覺(jué)得很沒(méi)有道理。按理ItemCreated是在初始化行之后,然后進(jìn)行找控件,應(yīng)該沒(méi)問(wèn)題。
這個(gè)問(wèn)題,想了很久,沒(méi)有結(jié)果,然后就和我一個(gè)同學(xué)(他技術(shù)不錯(cuò))開(kāi)始討論,剛開(kāi)始他說(shuō)是,就是說(shuō)當(dāng)運(yùn)行頁(yè)面的時(shí)候,在
去查看源碼帶的時(shí)候,發(fā)現(xiàn)DeleteButton這個(gè)控件的ID 變樣了,變成了ListView1_ctrl0_DeleteButton這樣子。對(duì)于這個(gè)問(wèn)題,
我一直到現(xiàn)在還沒(méi)有想通,不知道MS怎么會(huì)搞這個(gè)出來(lái)?服務(wù)器控件不是好好的渲染到頁(yè)面了,怎么變ID了呢?沒(méi)理由。呵呵~~
然后,我同學(xué)經(jīng)過(guò)調(diào)試后,給出了一個(gè)解決方案是:
復(fù)制代碼 代碼如下:
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;
}
}
}
這樣子就可以了。就是多加了一句:if (DeleteButton != null EditButton != null)的判斷。
方案二:
很有才的是,我宿舍的一位河北的同學(xué)也給出了一種解決方案,不過(guò)這種寫(xiě)法看起來(lái)有點(diǎn)不可思議,呵呵,一起看看
吧!
復(fù)制代碼 代碼如下:
asp:Button ID="DeleteButton" runat="server" Visible='%#Session["username"]=="admin"?true:false %>'
就是這樣子,也可以把控件給隱藏。因?yàn)閟ession是全局變量,所以可以在頁(yè)面中取到這個(gè)值。
作者:Lanny☆蘭東才
您可能感興趣的文章:- listview 選中高亮顯示實(shí)現(xiàn)方法
- Android ListView數(shù)據(jù)綁定顯示的三種解決方法
- Android通過(guò)LIstView顯示文件列表的兩種方法介紹
- android開(kāi)發(fā)教程之listview顯示sqlite數(shù)據(jù)
- Android ListView中動(dòng)態(tài)顯示和隱藏Header&Footer的方法
- Android編程實(shí)現(xiàn)Listview點(diǎn)擊展開(kāi)和隱藏的方法
- Android中ListView Item布局優(yōu)化技巧
- android動(dòng)態(tài)布局之動(dòng)態(tài)加入TextView和ListView的方法
- ListView的Adapter使用(綁定數(shù)據(jù)) 之 自定義每一項(xiàng)的布局去綁定數(shù)據(jù)
- 神奇的listView實(shí)現(xiàn)自動(dòng)顯示隱藏布局Android代碼