C# 单选按钮不';我无法在GridView中工作

C# 单选按钮不';我无法在GridView中工作,c#,gridview,radio-button,C#,Gridview,Radio Button,我有一个网格视图。每行都有一个文本框和一个单选按钮(3个选项) 如果选择单选按钮,则textbox.text=”“ 问题:当调用OnSelectedIndexChanged时,我网格中的每个文本框都变为空白 如何仅清除选中单选按钮所在行的文本框 ASPX标记 <asp:GridView id="mygrid" Runat="server"> <Columns> <asp:TemplateField>

我有一个网格视图。每行都有一个文本框和一个单选按钮(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>

您当前正在对每一行执行此操作,这将清除每个文本框。试试这个

 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
                }
            }
        }
    }

您当前正在对每一行执行此操作,这将清除每个文本框。试试这个

 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
                }
            }
        }
    }

您当前正在对每一行执行此操作,这将清除每个文本框。试试这个

 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
                }
            }
        }
    }

您当前正在对每一行执行此操作,这将清除每个文本框。试试这个

 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
                }
            }
        }
    }

您没有检查单选按钮列表是否有选定项。因此,您总是将文本框文本设置为空白。将功能更改为:

    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;
        }   
    }

您没有检查单选按钮列表是否有选定项。因此,您总是将文本框文本设置为空白。将功能更改为:

    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;
        }   
    }

您没有检查单选按钮列表是否有选定项。因此,您总是将文本框文本设置为空白。将功能更改为:

    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;
        }   
    }

您没有检查单选按钮列表是否有选定项。因此,您总是将文本框文本设置为空白。将功能更改为:

    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;
        }   
    }


向我们展示您的代码和您尝试的兴趣感谢…看一看谢谢marc_s这不是我的母语向我们展示您的代码和您尝试的兴趣感谢…看一看谢谢marc_s这不是我的母语向我们展示您的代码和您尝试的兴趣感谢…看一看谢谢marc_s这不是我的母语向我们展示您的代码和您尝试的内容感谢您的兴趣…看一看谢谢marc_这不是我的母语谢谢,非常有用没问题。你选择的答案有一个小问题,你应该注意。这样做,无论单击了哪个控件,都会发送一个事件,无论选择了哪个单选按钮列表,都会更新网格中的每个文本框。这与在顶部有一个按钮相同,该按钮在网格中循环并检查哪些按钮被选中,然后清除文本框。获取调用者只会影响您所在的行(进行选择时唯一关心的行)。注意:增加了一个休息时间。史蒂芬布里克纳的接球不错。我更新了我的答案,因此它首先获取所选行,然后清除文本框。这是因为您根本不做任何循环来查找所选行。@John Paul,SelectedRow有效地执行了相同的枚举,但这更简洁,做得好+谢谢你,非常有帮助没问题。你选择的答案有一个小问题,你应该注意。这样做,无论单击了哪个控件,都会发送一个事件,无论选择了哪个单选按钮列表,都会更新网格中的每个文本框。这与在顶部有一个按钮相同,该按钮在网格中循环并检查哪些按钮被选中,然后清除文本框。获取调用者只会影响您所在的行(进行选择时唯一关心的行)。注意:增加了一个休息时间。史蒂芬布里克纳的接球不错。我更新了我的答案,因此它首先获取所选行,然后清除文本框。这是因为您根本不做任何循环来查找所选行。@John Paul,SelectedRow有效地执行了相同的枚举,但这更简洁,做得好+谢谢你,非常有帮助没问题。你选择的答案有一个小问题,你应该注意。这样做,无论单击了哪个控件,都会发送一个事件,无论选择了哪个单选按钮列表,都会更新网格中的每个文本框。这与在顶部有一个按钮相同,该按钮在网格中循环并检查哪些按钮被选中,然后清除文本框。获取调用者只会影响您所在的行(进行选择时唯一关心的行)。注意:增加了一个休息时间。史蒂芬布里克纳的接球不错。我更新了我的答案,因此它首先获取所选行,然后清除文本框。这是因为您根本不做任何循环来查找所选行。@John Paul,SelectedRow有效地执行了相同的枚举,但这更简洁,做得好+谢谢你,非常有帮助没问题。你选择的答案有一个小问题,你应该注意。这样做,无论单击了哪个控件,都会发送一个事件,无论选择了哪个单选按钮列表,都会更新网格中的每个文本框。这与在顶部有一个按钮相同,该按钮在网格中循环并检查哪些按钮被选中,然后清除文本框。获取调用者只会影响您所在的行(进行选择时唯一关心的行)。注意:增加了一个休息时间。史蒂芬布里克纳的接球不错。我更新了我的答案,因此它首先获取所选行,然后清除文本框。这是因为您根本不做任何循环来查找所选行。@John Paul,SelectedRow有效地执行了相同的枚举,但这更简洁,做得好+谢谢,非常有帮助谢谢,非常有帮助谢谢,非常有帮助谢谢,非常有帮助