Asp.net 使用复选框启用/禁用GridView的按钮字段
我有网格视图Asp.net 使用复选框启用/禁用GridView的按钮字段,asp.net,gridview,c#-3.0,Asp.net,Gridview,C# 3.0,我有网格视图 其中一列是ItemTemplate列,其中包含复选框字段 其他2列是数据绑定列。其中一列是按钮字段,属于按钮类型 我希望此按钮最初设置为禁用模式 选中复选框后,应启用该特定行按钮字段。有人能帮忙吗 我的样品试一试 .aspx文件 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Email_Notificatio
- 其中一列是ItemTemplate列,其中包含复选框字段
- 其他2列是数据绑定列。其中一列是按钮字段,属于按钮类型
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Email_NotificationConnection %>"
SelectCommand="SELECT [Customer_Name] FROM [Customer]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" EnableModelValidation="True">
<Columns>
<asp:BoundField DataField="Customer_Name" HeaderText="Customer_Name"
SortExpression="Customer_Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" ID="non_prod_all_select" OnCheckedChanged="CheckBox2_CheckedChanged1" />
</ItemTemplate>
<HeaderStyle Width="30px" /></asp:TemplateField>
<asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Button" />
</Columns>
</asp:GridView>
请尝试使用以下代码: GridView1的ASPX代码:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Email_NotificationConnection %>"
SelectCommand="SELECT [Customer_Name] FROM [Customer]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" EnableModelValidation="True">
<Columns>
<asp:BoundField DataField="Customer_Name" HeaderText="Customer_Name"
SortExpression="Customer_Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" AutoPostBack="true" ID="non_prod_all_select" OnCheckedChanged="CheckBox2_CheckedChanged1" />
</ItemTemplate>
<HeaderStyle Width="30px" /></asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
所作的修改:
1.将复选框的AutoPostBack设置为true
2.删除了Button字段,并在网格的第三列中添加了一个带有Button的模板字段(以便asp:Button控件可以在代码隐藏中轻松读取)
3.更改代码后面的代码以执行必要的操作
注意:我已经在本地检查了这段代码,并按预期工作。因此,只要用此代码替换旧代码,并在出现任何问题时通知我。您尝试了什么???什么是.aspx。aspx.cs…?@Ganesh_Devlekar我什么都没试过。请帮助尝试一些东西并找出错误,我们在这里帮助您不要为您编写代码mate@Amitesh添加了代码。此代码中的错误出现在哪里
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Email_NotificationConnection %>"
SelectCommand="SELECT [Customer_Name] FROM [Customer]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" EnableModelValidation="True">
<Columns>
<asp:BoundField DataField="Customer_Name" HeaderText="Customer_Name"
SortExpression="Customer_Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" AutoPostBack="true" ID="non_prod_all_select" OnCheckedChanged="CheckBox2_CheckedChanged1" />
</ItemTemplate>
<HeaderStyle Width="30px" /></asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void CheckBox2_CheckedChanged1(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView3.Rows)
{
((Button)row.FindControl("Button1")).Enabled = ((CheckBox)row.FindControl("non_prod_all_select")).Checked;
}
}