[英]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。我采用的解决方案是:
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.