Asp.NET 下拉列表自动回发
我有一个启用了自动回发的下拉列表。并有一个中继器,我可以从数据库中填充复选框。
如果我将自动回发设置为 true,那么在选择值时复选框会失去其值...
对此有任何解决方法吗?
这是代码:
<asp:DropDownList ID="dropdown" runat="server" class="pop" AutoPostBack="true" >
</asp:DropDownList>
<asp:Repeater ID="rptD" runat="server" >
<ItemTemplate>
<td valign="top" >
<input type="checkbox" class="al" />
</ItemTemplate>
</asp:Repeater>
I have a dropdownlist with autopostback enabled. and have a repeater where i populate checkboxes from database.
If i set the autopostback to true then when selecting a value checkboxes lose its value...
Any workarounds on this?
Here is the code :
<asp:DropDownList ID="dropdown" runat="server" class="pop" AutoPostBack="true" >
</asp:DropDownList>
<asp:Repeater ID="rptD" runat="server" >
<ItemTemplate>
<td valign="top" >
<input type="checkbox" class="al" />
</ItemTemplate>
</asp:Repeater>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这是因为您不仅在
if(!IsPostBack)
上对中继器进行数据绑定,还在回发上进行数据绑定。因此,检查状态将被覆盖。因此,请在
Page_Load
中执行此操作(假设为 C#):而
DataBindRepeater
是设置 DataSource 属性和DataBind
Repeater 的方法。您可能还想使用 ASP.NET 复选框控件 而不是 html
input type="checkbox"
。仅当它是实现 IPostBackDataHandler.I assume this is because you are DataBinding the Repeater not only
if(!IsPostBack)
but also on postbacks. Therefore the checked state will be overriden.So do this in
Page_Load
(assuming C#):Whereas
DataBindRepeater
is a method that sets the DataSource property andDataBind
the Repeater.You might also want to use an ASP.NET Checkbox control instead of the html
input type="checkbox"
. The checked state is reloaded only if it's a server WebControl that implements IPostBackDataHandler.这听起来表明填充了 Page_Load 中的复选框。是这样吗?如果您要填充 Page_Load 中的控件,那么您需要将其包装在条件中:
否则,它们将在每次回发时重新填充。当您有自动回发或单击按钮或在启动回发的页面上执行某些其他操作时,将在事件处理程序之前调用 Page_Load。因此,实际上,发生了这种情况:
(旁注...请考虑使用 AJAX 进行动态客户端-服务器交互,这样的 Autopostback 会导致糟糕的用户体验,而且您会发现,这也会导致开发困难。 经验。)
This sounds indicative of populating the checkboxes in Page_Load. Is that the case? If you're populating the controls in Page_Load then you'll want to wrap it in a conditional:
Otherwise, they'll get re-populated with each postback. When you have an autopostback or click a button or perform some other action on the page which initiates a postback, Page_Load is called before the event handler. So in effect, this is happening:
(On a side note... Please look into using AJAX for dynamic client-server interaction like this. Autopostback makes for a poor user experience, and as you're discovering also makes for a difficult development experience.)