C# e、 Row.findcontrol()在数据绑定后返回null,但所有数据绑定字段都正常工作
我试图获得一个gridview来填充数据库调用中的文本到我的标签,如图所示 结果已经过测试,正在返回正确的名称C# e、 Row.findcontrol()在数据绑定后返回null,但所有数据绑定字段都正常工作,c#,asp.net,gridview,web-controls,C#,Asp.net,Gridview,Web Controls,我试图获得一个gridview来填充数据库调用中的文本到我的标签,如图所示 结果已经过测试,正在返回正确的名称 protected void Page_Load(object sender, EventArgs e) { DataTable t = DBProductLink.ListWithOptions(ProductId, LinkType, null); TestList.DataSource = t ; TestList.DataBind(); } 标签是在G
protected void Page_Load(object sender, EventArgs e)
{
DataTable t = DBProductLink.ListWithOptions(ProductId, LinkType, null);
TestList.DataSource = t ;
TestList.DataBind();
}
标签是在Gridview中创建的,如下所示:
<asp:GridView ID="TestList" runat="server" OnRowDataBound="testDataBound" AutoGenerateColumns="false" DataKeyNames="Id">
<Columns>
<asp:TemplateField HeaderText="Sizes">
<asp:ItemTemplate>
<asp:Label ID="sizeLabel" runat="server" Text='<%# Eval("size") %>' />
</asp:ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我使用的是完全相同的设置,在同一页面上的另一个gridview上有两个下拉框和两个标签,它们具有不同的名称,没有这个问题。有人对此有想法吗?
另一个Gridview如下所示:
<asp:GridView ID="SearchList" runat="server" AutoGenerateColumns="False"
DataKeyNames="Id" OnRowDataBound="SearchList_RowDataBound"
OnRowCommand="SearchList_RowCommand" Width="100%" PageSize="20" >
<Columns>
<asp:BoundField DataField="Code" HeaderText="Code" SortExpression="Code" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField HeaderText="Price" SortExpression="Price">
<ItemTemplate>
<robo:MoneyLabel ID="MoneyLabel2" runat="server"
Value='<%# Eval("Price") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<ItemTemplate>
<asp:Label ID="typeLabel" runat="server" Text='<%# Eval("Type") %>' />
<asp:HiddenField ID="productId" runat="server" Value='<%# Eval("Id") %>' />
<asp:HiddenField ID="isFabric" runat="server" Value='<%# Eval("IsFabric") %>' />
<asp:HiddenField ID="isOldWizard" runat="server" Value='<%# Eval("IsOldWizard") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Options/Color/Size">
<ItemTemplate>
<asp:LinkButton runat="server" ID="GetOptions" Text="Get Options" CausesValidation="false" CommandName="Options" />
<asp:Label ID="OptionLabel" Visible="false" runat="server" Text="Option: " />
<asp:DropDownList ID="ProductOptions" runat="server" Visible="false" />
<asp:Label ID="ColorLabel" Visible="false" runat="server" Text="Color: " />
<asp:DropDownList ID="RibbonColors" runat="server" Visible="false" AutoPostBack="true" />
<asp:Label ID="SizeLabel" Visible="false" runat="server" Text="Size: " />
<asp:DropDownList ID="RibbonSizes" runat="server" Visible="false" AutoPostBack="true" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Add">
<ItemStyle Width="60px" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text="Add" CommandName="Add" CommandArgument='<%# Eval("Id") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.cs是
protected void SearchList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;
int productId = (int)SearchList.DataKeys[e.Row.RowIndex].Value;
LinkButton GetOptions = e.Row.FindControl("GetOptions") as LinkButton;
DropDownList RibbonColors = e.Row.FindControl("RibbonColors") as DropDownList;
DropDownList RibbonSizes = e.Row.FindControl("RibbonSizes") as DropDownList;
DropDownList ProductOptions = e.Row.FindControl("ProductOptions") as DropDownList;
Label typeLabel = e.Row.FindControl("typeLabel") as Label;
HiddenField isFabric = e.Row.FindControl("isFabric") as HiddenField;
HiddenField isOldWizard = e.Row.FindControl("isOldWizard") as HiddenField;
ProductType typeValue = DBConvert.ToEnum<ProductType>(typeLabel.Text);
bool isFabricValue = Convert.ToBoolean(isFabric.Value.ToString());
bool isOldWizardValue = Convert.ToBoolean(isOldWizard.Value.ToString());
}
protectedvoid SearchList\u RowDataBound(对象发送方,GridViewRowEventArgs e)
{
if(e.Row.RowType!=DataControlRowType.DataRow)
返回;
int productId=(int)SearchList.DataKeys[e.Row.RowIndex].Value;
LinkButton GetOptions=e.Row.FindControl(“GetOptions”)作为LinkButton;
DropDownList RibbonColors=e.Row.FindControl(“RibbonColors”)作为DropDownList;
DropDownList RibbonSizes=e.Row.FindControl(“RibbonSizes”)作为DropDownList;
DropDownList ProductOptions=e.Row.FindControl(“ProductOptions”)作为DropDownList;
标签typeLabel=e.Row.FindControl(“typeLabel”)作为标签;
HiddenField isFabric=e.Row.FindControl(“isFabric”)作为HiddenField;
HiddenField isOldWizard=e.Row.FindControl(“isOldWizard”)作为HiddenField;
ProductType typeValue=DBConvert.ToEnum(typeLabel.Text);
bool isFabricValue=Convert.ToBoolean(isFabric.Value.ToString());
bool isOldWizardValue=Convert.ToBoolean(isOldWizard.Value.ToString());
}
我刚发现问题
你的标记是错误的,这很棘手。。。我承认
此标记:
应为
更改此项:
<asp:TemplateField HeaderText="Sizes">
<asp:ItemTemplate>
<asp:Label ID="sizeLabel" runat="server" Text='<%# Eval("size") %>' />
</asp:ItemTemplate>
</asp:TemplateField>
进入
这应该引起一个例外。。。但是,
ItemTemplate
被完全忽略事件称为RowDataBound
而不是datarowbound
,除此之外,您还没有在GridView上声明事件处理程序。旁注:您不应该在每次回发时在页面加载中对GridView进行数据绑定(默认情况下,当ViewState处于启用状态时)。相反,如果(!IsPostBack)@Tim感谢Tim,那么您应该只执行操作。Tim我使用了一系列尝试,并且多次使用了不同的方法,例如使用onrowdatabound以及直接执行TestList.DataBind();以及OnRowDataBound=“testDataBound”。我将编辑帖子以正确显示事件处理程序还有其他想法吗?你能粘贴其他正常工作的gridview的标记吗?@Andre我已经为你发布了另一个gridview,你可以看出我正在使用一个测试gridview来解决我的错误问题。我找不到你的代码有任何错误,它应该可以工作,从我的头顶上看,你确定你没有对多个GridView
使用同一个处理程序吗?Jupaol你也能看看我的问题吗。。。
<asp:TemplateField HeaderText="Sizes">
<asp:ItemTemplate>
<asp:Label ID="sizeLabel" runat="server" Text='<%# Eval("size") %>' />
</asp:ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sizes">
<ItemTemplate>
<asp:Label ID="sizeLabel" runat="server" Text='<%# Eval("size") %>' />
</ItemTemplate>
</asp:TemplateField>