C# 分别访问asp.net GridView中每行的文本框和下拉列表
我有一个网格视图如下C# 分别访问asp.net GridView中每行的文本框和下拉列表,c#,jquery,mysql,asp.net,gridview,C#,Jquery,Mysql,Asp.net,Gridview,我有一个网格视图如下 <asp:GridView ID="grid_flats_allflatslist" OnRowDeleting="grid_flats_allflatslist_RowDeleting" runat="server" Width="95%" CssClass="gridview" ShowFooter="true" AllowPaging="true" PageSize="8" BackColor="Black" BorderColor="#E7E7FF" Bord
<asp:GridView ID="grid_flats_allflatslist" OnRowDeleting="grid_flats_allflatslist_RowDeleting" runat="server" Width="95%" CssClass="gridview" ShowFooter="true" AllowPaging="true" PageSize="8" BackColor="Black" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="1" CellSpacing="1" frame="below" DataKeyNames="BlockID" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="BlockID" HeaderText="Block ID" Visible="false" />
<asp:BoundField DataField="BlockName" HeaderText="Block Name" />
<asp:TemplateField HeaderText="Flat Number">
<ItemTemplate>
<asp:TextBox ID="text_flats_listflatnumber" CssClass="textbox" onkeypress="return numeric(this);" MaxLength="5" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Flat Number">
<ItemTemplate>
<asp:DropDownList ID="list_flats_listflattype" CssClass="droplist" runat="server" AutoPostBack="true" OnSelectedIndexChanged="list_flats_listflattype_SelectedIndexChanged">
<asp:ListItem Text="Bachelor" Value="Bachelor"></asp:ListItem>
<asp:ListItem Text="Family" Value="Family"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Max Vacancy">
<ItemTemplate>
<asp:TextBox ID="text_flats_listmaxvacancy" onkeypress="return numeric(this);" MaxLength="2" CssClass="textbox" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="true" DeleteText="Delete" />
</Columns>
<FooterStyle BackColor="#9e4900" ForeColor="#fff1e5" Font-Bold="false" Font-Underline="false" HorizontalAlign="Center" />
<HeaderStyle BackColor="#9e4900" ForeColor="#fff1e5" HorizontalAlign="Center" />
<PagerStyle BackColor="#ff8080" ForeColor="#660000" HorizontalAlign="Right" />
<RowStyle BackColor="#ffe7d6" ForeColor="#660000" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="#ffd6ba" ForeColor="#660000" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#fff9f4" HorizontalAlign="Center" ForeColor="Black" />
</asp:GridView>
<asp:Button ID="button_flats_flatplus" CssClass="button" Height="30px" Width="25%" runat="server" Text="Add Flat" OnClick="button_flats_flatplus_Click" />
添加多行后,网格视图如下所示
protected void grid_flats_allflatslist_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
DataTable dt = ViewState["CurrentTable"] as DataTable;
dt.Rows[index].Delete();
ViewState["CurrentTable"] = dt;
grid_flats_allflatslist.DataSource = dt;
grid_flats_allflatslist.DataBind();
}
<asp:DropDownList ID="list_flats_listflattype" CssClass="droplist" runat="server">
<asp:ListItem Text="Bachelor" Value="Bachelor"></asp:ListItem
<asp:ListItem Text="Family" Value="Family"></asp:ListItem>
</asp:DropDownList>
如果我在这里按delete,那么它将被成功删除。现在,我将在文本框中输入值。但是,当我从第1行中选择“on Family”时,第1行的“Max Emptance”文本框应该被禁用,文本内容设置为-。如果我从第1行选择“单身汉”,则应启用第1行的“最大空缺”文本框,并清除文本以输入值。无论我在哪一行工作,都应该这样做。但我不知道如何才能做到这一点,因为我在网格视图中只有一个文本框ID,而且我不知道如何分别访问每一行元素
输入所有值后,我将单击一个保存按钮,该按钮将按网格视图中的方式逐行插入数据库中输入的所有值。这也是我不知道怎么做,因为我不能访问每行文本框分别。我有什么办法可以做到这一点吗
我的行删除代码如下:
protected void grid_flats_allflatslist_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
DataTable dt = ViewState["CurrentTable"] as DataTable;
dt.Rows[index].Delete();
ViewState["CurrentTable"] = dt;
grid_flats_allflatslist.DataSource = dt;
grid_flats_allflatslist.DataBind();
}
<asp:DropDownList ID="list_flats_listflattype" CssClass="droplist" runat="server">
<asp:ListItem Text="Bachelor" Value="Bachelor"></asp:ListItem
<asp:ListItem Text="Family" Value="Family"></asp:ListItem>
</asp:DropDownList>
将值插入数据库时,网格视图块ID的第一个绑定字段应该是主键。为了获得良好的可视性,我将其隐藏。尝试此操作以添加对单位数和最大空缺数的验证
$(document).on('change', '.droplist', function(){
var parentRow=$(this).parents("tr:eq(0)");
var text_flats_listmaxvacancy=$(parentRow).find('[id*="text_flats_listmaxvacancy"]');
var _value=$(this).val();
if(_value=="Family"){
$(text_flats_listmaxvacancy).val("--");
$(text_flats_listmaxvacancy).attr("readonly",true);
}
else if(_value=="Bachelor"){
$(text_flats_listmaxvacancy).val("");
$(text_flats_listmaxvacancy).attr("readonly",false);
}
});
//Code for save button to find each row and controls
foreach(GridViewRow row in grid_flats_allflatslist.Rows)
{
TextBox text_flats_listflatnumber=(TextBox)row.FindControl("text_flats_listflatnumber");
DropDownList list_flats_listflattype=(DropDownList)row.FindControl("list_flats_listflattype");
TextBox text_flats_listmaxvacancy=(TextBox)row.FindControl("text_flats_listmaxvacancy");
}
从下拉列表中删除AutoPostBack和OnSelectedIndexChanged,如下所示
protected void grid_flats_allflatslist_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
DataTable dt = ViewState["CurrentTable"] as DataTable;
dt.Rows[index].Delete();
ViewState["CurrentTable"] = dt;
grid_flats_allflatslist.DataSource = dt;
grid_flats_allflatslist.DataBind();
}
<asp:DropDownList ID="list_flats_listflattype" CssClass="droplist" runat="server">
<asp:ListItem Text="Bachelor" Value="Bachelor"></asp:ListItem
<asp:ListItem Text="Family" Value="Family"></asp:ListItem>
</asp:DropDownList>
很好,当我点击“家庭”时,文本正在更改,当我选择“单身汉”时,文本将被清除。但“启用”和“禁用”不会发生。我已更新代码并添加只读属性。如果有效,请尝试使用此选项。否仍不起作用,与之前相同启用/禁用不起作用尝试此$'text\u flats\u listmaxoutage'.attr'readonly',true;取代此$text\u flats\u listmaxoutage.attrreadonly,readonly;好了,现在它工作了:我从下拉列表中删除了AutoPostBack和OnSelectedIndexChanged,现在它工作了。非常感谢。