处理asp.net嵌套转发器中内部转发器复选框控件的checkedchanged事件

发布于 2024-11-28 19:51:36 字数 1708 浏览 0 评论 0原文

我在我的 aspx 页面上嵌套了转发器。在外部转发器中,我显示产品列表,在内部转发器中,我显示与每个产品关联的附加选项列表。内部转发器包含复选框、文本框、标签和其他当用户选择内部转发器中的复选框时,我想找到外部转发器内的控件。为了处理这个问题,我使用以下代码。

<asp:Repeater ID="OuterRepeater" runat="server" 
        onitemdatabound="OuterRepeater_ItemDataBound" >
        <ItemTemplate>
    <asp:Label ID="CodeLabel" runat="server" Text='<%# Eval("Code") %>'></asp:Label>
     <asp:Repeater ID="InnerRepeater" runat="server" OnItemCreated="InnerRepeater_ItemCreated">
       <ItemTemplate>
    <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true"/> 
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
      ......
      .......
    </ItemTemplate>
    </asp:Repeater>
     ......
      ......
    </ItemTemplate>
    </asp:Repeater>


 protected void InnerRepeater_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem ri = (RepeaterItem)e.Item;

            if (ri.ItemType == ListItemType.Item || ri.ItemType == ListItemType.AlternatingItem
            )
            {
                CheckBox cb = ri.FindControl("CheckBox1") as CheckBox;
                cb.CheckedChanged += new EventHandler(CheckBox1_CheckedChanged);
            }
        }

private void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {          

            CheckBox cb = (CheckBox)sender;
            if (cb.Checked)
            {
                //do something
            }
            else
            {
              //do something
            }
        }

但由于某种原因,复选框的 checkChanged 事件没有触发。而且我不确定如何在内部转发器复选框控件的选中更改事件中访问外部转发器的文本框。

有人可以帮我解决这个问题吗?

谢谢

I have nested repeaters on my aspx page.In the outer repeater I am displaying a list of products and in the inner repeater I am displaying a list of additional options associated with each product.The inner repeater contains a checkbox,textbox,label and other stuff.I would like to find the controls inside the outer repeater when a user selects a checkbox in the inner repeater.In order to handle this I am using the following code.

<asp:Repeater ID="OuterRepeater" runat="server" 
        onitemdatabound="OuterRepeater_ItemDataBound" >
        <ItemTemplate>
    <asp:Label ID="CodeLabel" runat="server" Text='<%# Eval("Code") %>'></asp:Label>
     <asp:Repeater ID="InnerRepeater" runat="server" OnItemCreated="InnerRepeater_ItemCreated">
       <ItemTemplate>
    <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true"/> 
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
      ......
      .......
    </ItemTemplate>
    </asp:Repeater>
     ......
      ......
    </ItemTemplate>
    </asp:Repeater>


 protected void InnerRepeater_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem ri = (RepeaterItem)e.Item;

            if (ri.ItemType == ListItemType.Item || ri.ItemType == ListItemType.AlternatingItem
            )
            {
                CheckBox cb = ri.FindControl("CheckBox1") as CheckBox;
                cb.CheckedChanged += new EventHandler(CheckBox1_CheckedChanged);
            }
        }

private void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {          

            CheckBox cb = (CheckBox)sender;
            if (cb.Checked)
            {
                //do something
            }
            else
            {
              //do something
            }
        }

But the checkedChanged event of the checkbox is not firing for some reason.Also I am not sure how to access the textbox of the outer repeater in the checked changed event of the innter repeater checkbox control.

Could someone please help me with this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

羁〃客ぐ 2024-12-05 19:51:36

它不会触发 CheckedChanged 事件,因为您已将事件处理程序声明为 private,您必须将其设为 Protected 或 Public

Protected void CheckBox1_CheckedChanged(object sender, EventArgs e)

您可以访问 Textbox 控件,例如...

private void CheckBox1_CheckedChanged(object sender, EventArgs e)
{ 
  CheckBox checkBox = (CheckBox)sender;
  Textbox textbox1 = (TextBox)checkBox.Parent.FindControl("TextBox1");
  String textboxText = textbox1.Text;
}

It does not fire the CheckedChanged event, since you have declared the event handler as private, You have to make it Protected or Public

Protected void CheckBox1_CheckedChanged(object sender, EventArgs e)

You can access the Textbox control like..

private void CheckBox1_CheckedChanged(object sender, EventArgs e)
{ 
  CheckBox checkBox = (CheckBox)sender;
  Textbox textbox1 = (TextBox)checkBox.Parent.FindControl("TextBox1");
  String textboxText = textbox1.Text;
}
怀中猫帐中妖 2024-12-05 19:51:36

您似乎没有在标记中定义事件处理程序。

<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" /> 

It doesn't look like you defined an event handler in your markup.

<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" /> 
入画浅相思 2024-12-05 19:51:36

Muhammad Akhtar 今天的回答对我帮助很大!
我只需要为我的中继器内的动态生成的复选框设置一个特定的 ID,以恢复事件的起源,并完成其余的处理,并且它运行得很好。

chkAtivo.ID = DataBinder.Eval(e.Item.DataItem, "id").ToString();

回收后与样品一样。
还不能投票,但谢谢你。

Muhammad Akhtar's answer helped me a lot today!
I just needed to set a specific ID to my dynamic generated checkboxes inside my reapeater to recover the origin of the event, and do the rest of the processing, and it worked perfectly.

chkAtivo.ID = DataBinder.Eval(e.Item.DataItem, "id").ToString();

Reovered just as the sample.
Cant vote up yet, but thank you.

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