单选按钮不工作在我的GridView
本文关键字:我的 GridView 工作 单选按钮 | 更新日期: 2023-09-27 18:14:35
我有一个GridView。每一行都有一个文本框和一个单选按钮(3个选项)
如果单选按钮被选中,则textbox.text = ""
问题:当OnSelectedIndexChanged
被调用时,我的网格中的每个文本框都变成空白
如何仅清除选中单选按钮所在行的文本框?
ASPX标记
<asp:GridView id="mygrid" Runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButtonList ID="hi" runat="server"
OnSelectedIndexChanged="zzz" AutoPostBack="true" />
<asp:TextBox ID="txPregoeiro" runat="server" Text="." />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
c#后台代码protected void zzz(object sender, EventArgs e)
{
foreach (GridViewRow _row in mygrid.Rows)
{
if (_row.RowType == DataControlRowType.DataRow)
{
RadioButtonList hi = (RadioButtonList)_row.FindControl("hi");
TextBox txPregoeiro = (TextBox)_row.FindControl("txPregoeiro");
txPregoeiro.Text = string.Empty;
}
}
}
您没有检查单选按钮列表是否有选中项。因此,您总是将文本框文本设置为空白。将函数改为:
GridViewRow _row = mygrid.SelectedRow;
if (_row.RowType == DataControlRowType.DataRow)
{
RadioButtonList hi = (RadioButtonList)_row.FindControl("hi");
if(hi.SelectedItem != null) //This checks to see if a radio button in the list was selected
{
TextBox txPregoeiro = (TextBox)_row.FindControl("txPregoeiro");
txPregoeiro.Text = string.Empty;
}
}
您目前正在为每一行执行此操作,这将清除每个文本框。试试吧。
protected void zzz(object sender, EventArgs e)
{
var caller = (RadionButtonList)sender;
foreach (GridViewRow _row in mygrid.Rows)
{
if (_row.RowType == DataControlRowType.DataRow)
{
RadioButtonList hi = (RadioButtonList)_row.FindControl("hi");
if(hi == caller)
{
TextBox txPregoeiro = (TextBox)_row.FindControl("txPregoeiro");
txPregoeiro.Text = string.Empty;
break; //a match was found break from the loop
}
}
}
}