问题描述
repeater控件中:<asp:LinkButtonID="addLnk"runat="server"CommandName="ADD"CommandArgument='<%#Eval("ID")%>'>加入试卷</asp:LinkButton><asp:LinkButtonID="delLnk"runat="server"CommandName="DEL"CommandArgument='<%#Eval("ID")%>'>移出试卷</asp:LinkButton>
Repeater事件protectedvoidRepeater1_ItemDataBound(objectsender,RepeaterItemEventArgse){//绑定数据到Repeater}protectedvoidRepeater1_ItemCommand(objectsender,RepeaterCommandEventArgse){//根据Linkbutton的Commandname的值来确定下面的操作:将Commandargument保存给session,并将设置相应Linkbutton的visible的隐藏或显示}
问题是,每次单击linkbutton时,页面的图片要重新加载一次,像刷新了一下似的
解决方案
解决方案二:
如果只是隐藏可以直接写JS
解决方案三:
单击的时候有个传值,后台存储
解决方案四:
这个事件ItemDataBound,第一次PostBack之后,均要执行。如果是ItemCreated的话,它只会执行一次。你可以尝试debug一下,它看看它是怎样跑的?morerefer:
解决方案五:
用AJAX吧。控件一般都会有回发的。最不济用个ajax控件
解决方案六:
我把代码贴出来,大家看看:.aspx:<asp:UpdatePanelID="UpdatePanel1"runat="server"><ContentTemplate><asp:RepeaterID="Repeater1"runat="server"OnItemDataBound="Repeater1_ItemDataBound"OnItemCommand="Repeater1_ItemCommand"><ItemTemplate><divclass="divSTBody"><divclass="divSTInfo"><table><tr><td>题型:<%#Eval("stTable")%></td><td>难度:<%#Eval("STLevel")%></td><td>知识模块:<%#Eval("RelateKnowledge")%></td><td>上传时间:<%#Eval("UploadTime")%></td></tr></table></div><divclass="divQuestion"id="question<%#Eval("id")%>"onclick="showOrhideDiv('answer<%#Eval("ID")%>')"><divid="STContentDiv"runat="server"></div></div><divclass="divAnswer"id="answer<%#Eval("ID")%>"style="display:none"><divid="STAnswerDiv"runat="server"><%#Eval("Answer")%></div></div><divid="addORdel_LnkBtn"class="divZJadd"><asp:LinkButtonID="addLnk"runat="server"CommandName="ADD"CommandArgument='<%#Eval("ID")%>'>加入试卷</asp:LinkButton><asp:LinkButtonID="delLnk"runat="server"CommandName="DEL"CommandArgument='<%#Eval("ID")%>'>移出试卷</asp:LinkButton></div></div></ItemTemplate></asp:Repeater></ContentTemplate></asp:UpdatePanel>
.cs:protectedvoidRepeater1_ItemDataBound(objectsender,RepeaterItemEventArgse){DataRowViewdrv=(DataRowView)e.Item.DataItem;LinkButtonaddLnk=e.Item.FindControl("addLnk")asLinkButton;LinkButtondelLnk=e.Item.FindControl("delLnk")asLinkButton;List<string>stIDArray;objectarrayVar=Session["stIDArray"];if(arrayVar==null||(arrayVarasList<string>)==null){stIDArray=newList<string>();}else{stIDArray=arrayVarasList<string>;}if(stIDArray.Contains(Convert.ToString(drv["ID"]))){addLnk.Visible=false;delLnk.Visible=true;}else{addLnk.Visible=true;delLnk.Visible=false;}//解析试题内容中的图片并设置src//HtmlGenericControldivTemp=e.Item.FindControl("STContentDiv")asHtmlGenericControl;divTemp.InnerHtml=htmlstr;}protectedvoidRepeater1_ItemCommand(objectsender,RepeaterCommandEventArgse){LinkButtonaddLnk=e.Item.FindControl("addLnk")asLinkButton;LinkButtondelLnk=e.Item.FindControl("delLnk")asLinkButton;List<string>stIDArray;objectarrayVar=Session["stIDArray"];if(arrayVar==null||(arrayVarasList<string>)==null){stIDArray=newList<string>();}else{stIDArray=arrayVarasList<string>;}if(e.CommandName=="ADD"){stringaddStr=Convert.ToString(e.CommandArgument);if(!stIDArray.Contains(addStr)){stIDArray.Add(addStr);addLnk.Visible=false;delLnk.Visible=true;}}else{if(e.CommandName=="DEL"){stringdelID=Convert.ToString(e.CommandArgument);if(stIDArray.Contains(delID)){stIDArray.Remove(delID);delLnk.Visible=false;addLnk.Visible=true;}}}Session["stIDArray"]=stIDArray;}
解决方案七:
Repeater中的Linkbutton每单击一次,就postback一次
解决方案八:
引用3楼insus的回复:
这个事件ItemDataBound,第一次PostBack之后,均要执行。如果是ItemCreated的话,它只会执行一次。你可以尝试debug一下,它看看它是怎样跑的?morerefer:
Repeater中的Linkbutton每单击一次,就postback一次,所以就再执行ItemDataBound一次,相当于重新加载一次了。你说的ItemCreated该怎么用
解决方案九:
用ajax来实现吧