C# 用于访问ItemTemplate中控件的Gridview事件&;EditItemTemplate?
是否有一个gridview事件可以访问C# 用于访问ItemTemplate中控件的Gridview事件&;EditItemTemplate?,c#,asp.net,gridview,visual-studio-2012,C#,Asp.net,Gridview,Visual Studio 2012,是否有一个gridview事件可以访问ItemTemplate和EditItemTemplate中的控件而无需附加代码(即会话、视图状态等) 假设我的gridview如下所示: <asp:GridView ID="GridView_Sales" runat="server" AutoGenerateColumns="False" DataKeyNames="SalesId" OnRowDataBound="OnRowDataBound" OnRowEditing="GridView_
ItemTemplate
和EditItemTemplate
中的控件而无需附加代码(即会话、视图状态等)
假设我的gridview如下所示:
<asp:GridView ID="GridView_Sales" runat="server"
AutoGenerateColumns="False"
DataKeyNames="SalesId"
OnRowDataBound="OnRowDataBound"
OnRowEditing="GridView_NSB_RowEditing"
OnRowUpdating="GridView_NSB_RowUpdating"
OnRowCommand="GridView_NSB_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Sold">
<ItemTemplate>
<asp:Label ID="Label_WasSold" runat="server" Text='<%# Eval("WasSold").ToString() %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList_Sold" runat="server">
<asp:ListItem Value="Yes"> </asp:ListItem>
<asp:ListItem Value="No"> </asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
GridView\u RowDataBound
可以访问itemtemplate
中的Label\u wassell
,但不能访问EditItemTemplate
中的下拉列表GridView_RowEditing
可以访问DropDownList_Saled
,但不能访问Label_Wassell
;GridView\u rowupdateing
也一样
我想将
Label\u wassell.Text
中的值与DropDownList\u sall中的值进行比较。在执行更新时,无需添加更多代码或将会话变量从一个位置拖到另一个位置。只需向EditTemplate添加一个隐藏字段,将wassell
数据项的值存储为code中的值下面
在行更新
事件中,可以找到隐藏字段并获取其值,然后将其与下拉列表值进行比较
在EditTemplate中包含隐藏字段的标记
我以为除了
HiddenField,还有另一种方法,但那是可行的。谢谢
<asp:TemplateField HeaderText="Sold">
<ItemTemplate>
<asp:Label ID="Label_WasSold" runat="server" Text='<%# Eval("WasSold").ToString() %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField id="hdnWasSold" runat="server" Value='<%# Eval("WasSold").ToString() %>' />
<asp:DropDownList ID="DropDownList_Sold" runat="server">
<asp:ListItem Value="Yes"> </asp:ListItem>
<asp:ListItem Value="No"> </asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
HiddenField hdnWasSold = (HiddenField)GridView_Sales.Rows[e.RowIndex].FindControl("hdnWasSold");
string wasSoldValue = hdnWasSold.Value;