简体   繁体   English

如何在编辑项模板中找到控件?

[英]how to find control in edit item template?

i have a gridview on the form and have some template field, one of them is:我在表格上有一个 gridview 并且有一些模板字段,其中之一是:

<asp:TemplateField HeaderText="Country" HeaderStyle-HorizontalAlign="Left">
    <EditItemTemplate>
        <asp:DropDownList ID="DdlCountry" runat="server" DataTextField="Country" DataValueField="Sno">
        </asp:DropDownList>
    </EditItemTemplate>
    </asp:TemplateField>

now on the RowEditing event i need to get the selected value of dropdownlist of country and then i will set that value as Ddlcountry.selectedvalue=value;现在在 RowEditing 事件中,我需要获取国家/地区下拉列表的选定值,然后将该值设置为 Ddlcountry.selectedvalue=value; so that when dropdownlist of edit item template appears it will show the selected value not the 0 index of dropdownlist.这样当出现编辑项模板的下拉列表时,它将显示所选值而不是下拉列表的 0 索引。 but i am unable to get the value of dropdown list.但我无法获得下拉列表的值。 i have tried this already:我已经试过了:

int index = e.NewEditIndex;
DropDownList DdlCountry = GridView1.Rows[index].FindControl("DdlCountry") as DropDownList;

need help please.请需要帮助。 thanx.谢谢。

You need to databind the GridView again to be able to access the control in the EditItemTemplate .您需要再次对GridView进行数据绑定才能访问EditItemTemplate的控件。 So try this:所以试试这个:

int index = e.NewEditIndex;
DataBindGridView();  // this is a method which assigns the DataSource and calls GridView1.DataBind()
DropDownList DdlCountry = GridView1.Rows[index].FindControl("DdlCountry") as DropDownList;

But instead i would use RowDataBound for this, otherwise you're duplicating code:但相反,我会为此使用RowDataBound ,否则您将重复代码:

protected void gridView1_RowDataBound(object sender, GridViewEditEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
  {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
          DropDownList DdlCountry = (DropDownList)e.Row.FindControl("DdlCountry");
          // bind DropDown manually
          DdlCountry.DataSource = GetCountryDataSource();
          DdlCountry.DataTextField = "country_name";
          DdlCountry.DataValueField = "country_id";
          DdlCountry.DataBind();

          DataRowView dr = e.Row.DataItem as DataRowView;
          Ddlcountry.SelectedValue = value; // you can use e.Row.DataItem to get the value
        }
   }
}

You can try wit this code - based on EditIndex property您可以尝试使用此代码 - 基于EditIndex property

var DdlCountry  = GridView1.Rows[GridView1.EditIndex].FindControl("DdlCountry") as DropDownList;

Link : http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.gridview.editindex.aspx链接: http : //msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.gridview.editindex.aspx

I looked at the controls in scope while debugging this issue for selecting a drop down item in a DropDownList in an EditItemTemplate.我查看了 scope 中的控件,同时调试此问题以在 EditItemTemplate 的 DropDownList 中选择下拉项。 The issue here, the data is not part of the HTML in the initial form and when the OnRowEditing posts back, there is no information about those edit controls.这里的问题是,数据不是初始表单中 HTML 的一部分,当 OnRowEditing 回发时,没有关于这些编辑控件的信息。

If you use the F12 developer tools on your browser page before the OnRowEditing you will not see the editing control as ASP did not written them to the HTML with the initial data bind;如果您在 OnRowEditing 之前在浏览器页面上使用 F12 开发人员工具,您将看不到编辑控件,因为 ASP 没有使用初始数据绑定将它们写入 HTML; this keeps the form data as small as possible.这使表单数据尽可能小。 After OnRowEditing, the return will show the editing controls as ASP has now written them to the HTML. The solution I went with was:在 OnRowEditing 之后,返回将显示编辑控件,因为 ASP 现在已将它们写入 HTML。我采用的解决方案是:

  1. Get the 'selection' information from the current GridView which held the initial data bind and store it in a local string.从保存初始数据绑定的当前 GridView 中获取“选择”信息,并将其存储在本地字符串中。
  2. Overwrite the GridView with the new 'Editing' data with a new data bind.使用新的数据绑定用新的“编辑”数据覆盖 GridView。
  3. Get the DropDownList control and set it to the local 'selection' string.获取 DropDownList 控件并将其设置为本地“选择”字符串。
protected void gv_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
    {
        try
        {
            gv.EditIndex = e.NewEditIndex; // row index being edited

            string businessSelection = (gv.Rows[e.NewEditIndex].FindControl("lblFieldName") as Label).Text;

            gv.DataSource = GetBusinessData(); gv.DataBind();

            (gv.Rows[e.NewEditIndex].FindControl("ddlFieldName") as DropDownList).SelectedValue = businessSelection;
        }
        catch (Exception ex) { ErrorHandleAndLog(ex); }
    }

The field markup looks like this (internal naming conventions and data values removed, of course):字段标记如下所示(当然,删除了内部命名约定和数据值):

<%-- Field Comment --%>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center">
  <HeaderTemplate>
    <asp:Label runat="server" Text="Field Name" />
  </HeaderTemplate>
  <ItemTemplate>
    <asp:Label runat="server" ID="lblFieldName" Text='<%# Eval("FieldName") %>' />
  </ItemTemplate>
  <EditItemTemplate>
    <asp:DropDownList runat="server" ID="ddlFieldName" >
      <asp:ListItem Text="" />
      <asp:ListItem Text="Business Option 1" />
      <asp:ListItem Text="Business Option 2" />
      <asp:ListItem Text="Business Option 3" />
    </asp:DropDownList>
  </EditItemTemplate>
  <FooterTemplate>
    <asp:DropDownList runat="server" ID="ddlNewFieldName">
      <asp:ListItem Text="" />
      <asp:ListItem Text="Business Option 1" />
      <asp:ListItem Text="Business Option 2" />
      <asp:ListItem Text="Business Option 3" />
    </asp:DropDownList>
  </FooterTemplate>
</asp:TemplateField>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Gridview中找不到控件编辑项目模板 - Can't Find Control Within Gridview Edit Item Template 如何在FormView项目模板中找到控件 - How to find control in formview item template 如何在添加新项对话框中找到WPF自定义控件模板? - How to find the WPF Custom Control template in Add new Item dialog? 在GridView中查找项目模板控件的行 - Find row of an item template control in gridview 如何获取放置在javascript中gridview的Edit item模板内的控件的clientid - How to get the clientid of the control which is placed inside the Edit item template of the gridview in javascript uwp:如何在表单控件中编辑数据网格项 - uwp: how to edit a datagrid item in a form control 在行命令中找不到编辑模板控件复选框 - unable to find edit template control checkbox in row command 我的查找控件未在gridview项模板中找到标签,有什么主意我该如何解决? - My find control isn't finding a label in gridview item template, any ideas how can I fix this? WinRT XAML在FlipView项模板中查找控件 - WinRT XAML Find control inside FlipView Item template 对于用户控件,如何设置项目模板项目与用户属性的绑定? - For a user control, how to set the binding of a item template item to a user property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM