Asp.NET 下拉列表自动回发

发布于 2024-12-22 02:10:17 字数 485 浏览 2 评论 0原文

我有一个启用了自动回发的下拉列表。并有一个中继器,我可以从数据库中填充复选框。

如果我将自动回发设置为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帝王念 2024-12-29 02:10:17

我认为这是因为您不仅在 if(!IsPostBack) 上对中继器进行数据绑定,还在回发上进行数据绑定。因此,检查状态将被覆盖。

因此,请在 Page_Load 中执行此操作(假设为 C#):

if(!IsPostBack){
   DataBindRepeater();
}

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#):

if(!IsPostBack){
   DataBindRepeater();
}

Whereas DataBindRepeater is a method that sets the DataSource property and DataBind 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.

对风讲故事 2024-12-29 02:10:17

这听起来表明填充了 Page_Load 中的复选框。是这样吗?如果您要填充 Page_Load 中的控件,那么您需要将其包装在条件中:

if (!IsPostBack)
{
  // populate your controls from data
}

否则,它们将在每次回发时重新填充。当您有自动回发或单击按钮或在启动回发的页面上执行某些其他操作时,将在事件处理程序之前调用 Page_Load。因此,实际上,发生了这种情况:

  1. 用户导航到页面
  2. Page_Load 清除并填充复选框
  3. 用户在 DropDownList 中选择一个项目(触发回发)
  4. Page_Load 清除并填充复选框
  5. DropDownList 自动回发处理程序被调用

(旁注...请考虑使用 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:

if (!IsPostBack)
{
  // populate your controls from data
}

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:

  1. User navigates to the page
  2. Page_Load clears and populates the checkboxes
  3. User chooses an item in the DropDownList (triggering a postback)
  4. Page_Load clears and populates the checkboxes
  5. DropDownList autopostback handler is called

(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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文