获取DetailsView和UpdatePanel之间的控件ClientID/UniqueID

发布于 2024-09-03 04:20:10 字数 1398 浏览 0 评论 0原文

我想知道如何获取Detailsview 控件EditItemTemplate 元素内控件的ClientID/UniqueID,以及当DetailsViews 更改为编辑模式且DetailsView 位于AJAX UpdatePanel 内时。如果没有 UpdatePanel,在 PostBack 期间我可以获得 ClientID 的控制权,但现在有了 UpdatePanel。

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="SqlDataSource1" AllowPaging="true" AutoGenerateEditButton="true">
                <Fields>
                    <asp:TemplateField>
                        <EditItemTemplate>
                            <asp:CheckBox runat="server" ID="chkboxTest" Text="CHECKBOX" />
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Fields>
            </asp:DetailsView>
        </ContentTemplate>
    </asp:UpdatePanel>

如您所见,EditItemTemplate 包含一个复选框控件。因此,当“详细信息视图”更改为“编辑”模式时,我尝试获取此复选框的 ClientID。我需要这个值来处理 Javascript。

捕获事件ChangingMode/ChangedMode不起作用; chkbox 为空:

 void DetailsView1_ModeChanged(object sender, EventArgs e)
    {
        if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
        {
            var chkbox = DetailsView1.FindControl("chkboxTest"); // <==  is null
        }
    }

也许我使用了错误的事件?有人可以给我一个关于这个的提示吗?谢谢。

I like to know how to get the ClientID/UniqueID of a control inside a Detailsview controls EditItemTemplate element and when DetailsViews changing to Edit mode and DetailsView is inside a AJAX UpdatePanel. Without UpdatePanel, during PostBack I can get the ClientID's control, but now with an UpdatePanel.

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="SqlDataSource1" AllowPaging="true" AutoGenerateEditButton="true">
                <Fields>
                    <asp:TemplateField>
                        <EditItemTemplate>
                            <asp:CheckBox runat="server" ID="chkboxTest" Text="CHECKBOX" />
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Fields>
            </asp:DetailsView>
        </ContentTemplate>
    </asp:UpdatePanel>

As you see, the EditItemTemplate contains a Checkbox control. So i'm trying to get the ClientID of this checkbox when Detailsview is changing to the Edit mode. I need this value for handling Javascript.

Catching the events ChangingMode/ChangedMode doesn't work; chkbox is null:

 void DetailsView1_ModeChanged(object sender, EventArgs e)
    {
        if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
        {
            var chkbox = DetailsView1.FindControl("chkboxTest"); // <==  is null
        }
    }

Maybe i'm using the wrong event? Someone can give me a tip about this? Thanks.

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

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

发布评论

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

评论(2

断念 2024-09-10 04:20:10

好的,最好的办法是为 OnDataBound 实现一个处理程序,然后执行以下操作:

  protected void databound(object sender, EventArgs e)
  {
     if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
     {
        var control = DetailsView1.Rows[0].Cells[1].FindControl("chkboxTest");
        if (control != null)
        {
           // Write some JS...
        }
     }
  }

Ok, the best thing to do is implement a handler for OnDataBound, then do something like:

  protected void databound(object sender, EventArgs e)
  {
     if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
     {
        var control = DetailsView1.Rows[0].Cells[1].FindControl("chkboxTest");
        if (control != null)
        {
           // Write some JS...
        }
     }
  }
谁的新欢旧爱 2024-09-10 04:20:10

void DetailsView1_ModeChanged(对象发送者,EventArgs e)
{
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
var chkbox = DetailsView1.Rows[0].FindControl("chkxboxTest"); // <== 为空

加粗的文本是拼写错误吗

我没有太多使用DetailsView,但使usre Rows[0] 不是标题行,行下是否有任何单元格?就像网格视图一样。

更新:我假设您想要做的就是在用户更新项目后捕获控件?将事件处理程序分配给 OnItemUpdating 并尝试以下操作:

protected void updating(object sender, DetailsViewUpdateEventArgs e)
{
   var control = DetailsView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[1].FindControl("chkboxTest");
}

void DetailsView1_ModeChanged(object sender, EventArgs e)
{
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
var chkbox = DetailsView1.Rows[0].FindControl("chkxboxTest"); // <== is null
}

Is the enboldened text a typo?

I haven't had much use of the DetailsView but make usre Rows[0] is not a header row, and are there any Cells under the rows? Like the GridView.

UPDATE: I'm assuming all you want to do is capture the control after the user has updated the items? Assign an event handler to OnItemUpdating and try the following:

protected void updating(object sender, DetailsViewUpdateEventArgs e)
{
   var control = DetailsView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[1].FindControl("chkboxTest");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文