问题描述
之前我说过做一个留言板,在datalist里嵌套控件以使其可以:显示一排留言,留言下面有对应的按钮,按下按钮后,通过控制其下面控件的visible来在每条留言下面控制其相应回复是否显示。实现代码如下,但我无论按哪个留言下面的按钮它展开的都是第一条留言下面的回复,我想是我里面嵌套的控件的ID限定了的关系,但不限定的话太后又不能获取其值,求教怎么才能实现我要的功能?前台代码:<asp:DataListID="DataList1"runat="server"DataSourceID="SqlDataSource1"BackColor="White"BorderColor="White"BorderStyle="Ridge"BorderWidth="2px"CellPadding="3"CellSpacing="1"><HeaderStyleBackColor="#4A3C8C"Font-Bold="True"ForeColor="#E7E7FF"/><HeaderTemplate></HeaderTemplate><ItemStyleBackColor="#DEDFDE"ForeColor="Black"/><ItemTemplate>标题:<%#DataBinder.Eval(Container.DataItem,"title")%> <%#DataBinder.Eval(Container.DataItem,"name")%>于<%#DataBinder.Eval(Container.DataItem,"date")%>发表<br/>留言:<%#DataBinder.Eval(Container.DataItem,"word")%><br/><asp:LabelID="Label1"runat="server"Visible="false"Text='<%#Bind("id")%>'></asp:Label><asp:LinkButtonID="Button1"Visible="true"runat="server"OnClick="Button1_Click">查看回复</asp:LinkButton><asp:LinkButtonID="Button3"Visible="false"runat="server"OnClick="Button3_Click">收起回复</asp:LinkButton><br/><asp:Panelrunat="server"ID="Panel1"visible="false"><asp:TextBoxID="TextBox1"runat="server"Height="41px"TextMode="MultiLine"Width="1005px"ReadOnly="True"></asp:TextBox><br/><asp:ButtonID="Button2"runat="server"Text="回复"onclick="Button2_Click"/><br/>回复:<br/><asp:DataListID="DataList2"runat="server"><HeaderTemplate></HeaderTemplate><ItemTemplate><spanclass="style4"><%#DataBinder.Eval(Container.DataItem,"name")%>:</span><%#DataBinder.Eval(Container.DataItem,"word")%>(<%#DataBinder.Eval(Container.DataItem,"date")%>)<br/></ItemTemplate><FooterTemplate></FooterTemplate></asp:DataList></asp:Panel><br/></ItemTemplate><FooterStyleBackColor="#C6C3C6"ForeColor="Black"/><FooterTemplate></FooterTemplate><SelectedItemStyleBackColor="#9471DE"Font-Bold="True"ForeColor="White"/></asp:DataList><asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ConnectionStrings:leavewordConnectionString4%>"SelectCommand="SELECT*FROM[leaveword]"></asp:SqlDataSource>
后台代码:protectedvoidPage_Load(objectsender,System.EventArgse){}protectedvoidButton2_Click(objectsender,EventArgse){stringstr="DataSource=(local);InitialCatalog=leaveword;integratedsecurity=True";SqlConnectionconn=newSqlConnection(str);conn.ConnectionString=str;conn.Open();LabelLabel1=(Label)DataList1.Items[0].FindControl("Label1");stringleavewordid=Label1.Text;TextBoxTextBox1=(TextBox)DataList1.Items[0].FindControl("TextBox1");stringTextBox1_text=TextBox1.Text;stringname=Session["membername"].ToString();stringdate=System.DateTime.Now.ToString("yyyy-MM-ddhh:mm:ss");stringip=(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]!=null&&HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]!=String.Empty)?HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]:HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];stringsql="Insertinto[reply](leavewordid,name,date,ip,word)values('"+leavewordid+"','"+name+"','"+date+"','"+ip+"','"+TextBox1_text+"')";SqlCommandcmd=newSqlCommand(sql,conn);cmd.ExecuteNonQuery();conn.Close();Response.Write("<Scriptlanguage=JavaScript>alert('更新成功');window.location.href='?act=';</script>");Response.Redirect("show.aspx");}protectedvoidButton1_Click(objectsender,EventArgse){if(Session["membername"]!=null){PanelPanel1=(Panel)DataList1.Items[0].FindControl("Panel1");LinkButtonButton1=(LinkButton)DataList1.Items[0].FindControl("Button1");LinkButtonButton3=(LinkButton)DataList1.Items[0].FindControl("Button3");DataListDataList2=(DataList)DataList1.Items[0].FindControl("DataList2");LabelLabel1=(Label)DataList1.Items[0].FindControl("Label1");stringleavewordid=Label1.Text;stringstr1="DataSource=(local);InitialCatalog=leaveword;integratedsecurity=True";SqlConnectionconn=newSqlConnection(str1);conn.Open();DataSetds2=newDataSet();stringstr2="Select*From[reply]Whereleavewordid='"+leavewordid+"'";SqlDataAdapterda2=newSqlDataAdapter(str2,conn);da2.Fill(ds2,"t1");DataList2.DataSource=ds2.Tables["t1"];DataList2.DataBind();Panel1.Visible=true;Button1.Visible=false;Button3.Visible=true;conn.Close();}else{Response.Write("<scriptlanguage='javascript'>alert('登录后才可回复!')</script>");}}protectedvoidButton3_Click(objectsender,EventArgse){PanelPanel1=(Panel)DataList1.Items[0].FindControl("Panel1");LinkButtonButton1=(LinkButton)DataList1.Items[0].FindControl("Button1");LinkButtonButton3=(LinkButton)DataList1.Items[0].FindControl("Button3");Panel1.Visible=false;Button1.Visible=true;Button3.Visible=false;}
解决方案
本帖最后由 crx0607 于 2012-01-17 23:22:55 编辑
解决方案二:
不要在按钮Click事件里查找控件来控制是否显示,GridView控件里用RowCommand事件,Repeater控件用ItemCommand事件,DataList我忘了事件名叫什么,按钮的CommandName里加上个参数来标识唯一ID什么的,在事件里e.CommandName可以捕获到该值!再进一步处理,手边没有VS没法帮你实验一下~
解决方案三:
直接用repeater吧不会产生任何垃圾代码无viwstate垃圾代码
解决方案四:
这种功能最后在前台用js或jquery来做。绑定数据时都绑定好,一开始不需要显示的给个样式display=“none”,id的话你可以用留言ID或者自己定义一个递增的数值,不重复就行,前面给个前缀,比如“div”。然后js里写方法:functionshow(i){ if(document.getElementById("div"+i).style.display=="none") { document.getElementById("div"+i).style.display=""; } else { document.getElementById("div"+i).style.display="none"; }}引用的时候:onclick='show(<%#Eval("id")%>)'