问题:我在datalist中嵌套了linkbutton,我想通过点击它来控制一个panel控件的隐现,但我试了好几种方法都不行,请大家帮我看看什么原因,也期待大家给出自己的解决方案,谢谢!
以下是我尝试过的四种方法:
前台:
<asp:DataList ID="gbDl" runat="server" RepeatLayout="Flow" Width="800px" DataKeyField="CommentId" OnItemCommand="gbDl_ItemCommand"
OnSelectedIndexChanged="gbDl_SelectedIndexChanged" >
<ItemTemplate>
<asp:Label ID="lbId" runat=server Text=' <%#Eval("CommentId")%>'> </asp:Label>
<asp:Label ID="lbname" runat="server" Text=' <%#Eval("PostName") %>'> </asp:Label>
<asp:Label ID="PostTime" runat="server" text=' <%#Eval("PostTime")%>'> </asp:Label>
<asp:LinkButton ID="lbReply" Text="回复" CommandName="reply" runat="server" > </asp:LinkButton>
<asp:Panel ID="plReply" Width="400px" runat="server" Visible=false>
<asp:TextBox ID="tbReply" Columns="50" Rows="10" runat=server TextMode=MultiLine> </asp:TextBox>
<asp:Button ID="btnSumit" runat=server CommandName="Submit" Text="提交" />
<asp:Button ID="btnCancle" runat=server CommandName="Cancle" Text="取消" />
</asp:Panel>
</ItemTemplate>
</asp:DataList>
后台:
方法一:
protected void gbDl_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "reply")
{
LinkButton lbtnRe = ((LinkButton)e.Item.FindControl("lbReply"));
lbtnRe.Attributes.Add("Click", "OnClickReply");
}
}
public void OnClickReply()
{
((Panel)gbDl.SelectedItem.FindControl("plReply")).Visible = true;
}
方法二:用这个方法时 我是将CommandName设置为:Select的,通常,datalist会自动调用这个事件的,但实践证明失败了~~
protected void gbDl_SelectedIndexChanged(object sender, EventArgs e)
{
Panel plRe = (Panel)gbDl.Items[gbDl.SelectedIndex].FindControl("plReply");
plRe.Visible = true;
}
下面这两种方法,我直接拿linkbutton开刀,但还是不行 -_-!!
方法三:
protected void lbReply_Command(object sender,CommandEventArgs e)
{
((Panel)gbDl.SelectedItem.FindControl("plReply")).Visible = true;
}
方法四: protected void lbReply_Command(object sender,EventArgs e)
{
((Panel)gbDl.SelectedItem.FindControl("plReply")).Visible = true;
}
有劳大家帮我看看了~~~再次感谢!!
第一个方法直接改为这样不行吗?:
protected void gbDl_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "reply")
{
var p= e.Item.FindControl("plReply");
p.Visible=!p.Visible;
}
}
protected void gbDl_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "reply")
{
((Panel)e.Item.FindControl("plReply")).Visible = true
}
}
试试看吧,跟踪下看是否能获得panel的引用。
var报错是因为可能你的.NET Framework为2.0或版本。
楼上的方法只能显示,不能隐藏。修改一下:
protected void gbDl_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "reply")
{
Panel p= (Panel)e.Item.FindControl("plReply");
p.Visible=!p.Visible;
}
}