C# RowDataBound事件中的FindControl以错误结束
MyC# RowDataBound事件中的FindControl以错误结束,c#,asp.net,findcontrol,C#,Asp.net,Findcontrol,MyGridView中的MyTemplateField是这样创建的: <asp:TemplateField HeaderText="Dienstleistung" SortExpression="gutscheinbezeichnung" HeaderStyle-Width="20px"> <EditItemTemplate> <asp:HiddenField runat="server" Value='<%#
GridView
中的MyTemplateField
是这样创建的:
<asp:TemplateField HeaderText="Dienstleistung" SortExpression="gutscheinbezeichnung" HeaderStyle-Width="20px">
<EditItemTemplate>
<asp:HiddenField runat="server" Value='<%# Bind("gutscheinart_id")%>' ID="HiddenFieldGutscheinartID"/>
<asp:DropDownList ID="DropDownListDienstleistung" ClientIDMode="Static" runat="server" DataSourceID="ObjectDataSourceDropDown" DataValueField="gutscheinbezeichnung">
</asp:DropDownList>
<asp:ObjectDataSource ID="ObjectDataSourceDropDown" runat="server" SelectMethod="GetGutscheinArt" TypeName="Gmos.Halbtax.Admin.Client.WebGui.DataManager"></asp:ObjectDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelGutscheinbezeichnung" runat="server" Text='<%# Bind("gutscheinbezeichnung") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Width="20px" />
</asp:TemplateField>
现在,如果此事件触发。发生以下错误:
索引超出范围。必须为非负数且小于
收藏。参数名称:索引
有什么建议吗?您在标题中找不到下拉控件,所以您需要检查当前行it
datarow
试试这个
protected void GridViewLehrling_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (GridViewLehrling.Rows.Count > 0)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList DropDownListDienstleistungBackEnd = (DropDownList)GridViewLehrling.Rows[GridViewLehrling.SelectedIndex].FindControl("DropDownListDienstleistung");
HiddenField HiddenFieldGutscheinartIDBackEnd = (HiddenField)GridViewLehrling.Rows[GridViewLehrling.EditIndex].FindControl("HiddenFieldGutscheinartID");
}
}
}
尝试使用以下代码:
protected void GridViewLehrling_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Edit)
{
DropDownList ddlBackEnd = (DropDownList)e.Row.FindControl("DropDownListDienstleistung");
HiddenField hdnBackEnd = (HiddenField)e.Row.FindControl("HiddenFieldGutscheinartID");
}
}
}
代码首先检查行的长度。它必须是
DataRow
,以便排除页脚行和页眉行。然后代码检查行是否实际处于编辑模式。如果是这种情况,那么代码将在实际行上获取执行FindControl
的控件。对我不起作用。错误仍然是一样的。这正是我要找的。谢谢
protected void GridViewLehrling_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Edit)
{
DropDownList ddlBackEnd = (DropDownList)e.Row.FindControl("DropDownListDienstleistung");
HiddenField hdnBackEnd = (HiddenField)e.Row.FindControl("HiddenFieldGutscheinartID");
}
}
}