【问题标题】:Get value from gridview column that has radio button using javascript in asp.net c#在asp.net c#中使用javascript从具有单选按钮的gridview列中获取值
【发布时间】:2014-06-29 19:42:50
【问题描述】:

这是我的带有单选按钮的网格视图

<asp:GridView ID="GridView1" ShowHeader="False" runat="server" cellpadding="5"
              cellspacing="1" ForeColor="#333333" GridLines="None" 
              OnRowDataBound="GridView1_RowDataBound" 
              OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="512px">
      <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:TemplateField>
                 <ItemTemplate>
                <asp:RadioButton ID="rbt"  onclick="javascript:Selrdbtn(this.id)"  runat="server" />
            </ItemTemplate>
                </asp:TemplateField>
            </Columns>
                <EditRowStyle BackColor="#999999" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
            </asp:GridView>

它调用这个脚本,它只帮助选择一个单选按钮

  <script language="javascript" type="text/javascript">

      function Selrdbtn(id) {
          var rdBtn = document.getElementById(id);

          var grid = document.getElementById('<%= GridView1.ClientID %>');

        var List = grid.getElementsByTagName("input");
        for (i = 0; i < List.length; i++) {
            if (List[i].type == "radio" && List[i].id != rdBtn.id) {
                List[i].checked = false;
            }
        }
    }

</script>

如何获取选中单选按钮的行值?

【问题讨论】:

  • 如果你能展示如何调用 Selrdbtn(id) 会有所帮助,你能发布吗?
  • 您想在 JavaScript(cient 端)还是 Code behind(服务器端)中获取行值?
  • 行值到底是什么意思?您想要选中的行中的表格单元格的内容吗? Jquery 库在这里也很有帮助,你以前用过还是愿意使用它?我看到您已将问题标记为 jQuery,但您的代码更直接 javascript。
  • @sh1rts 这里被调用了。
  • @AliShahrokhi 来自服务器端的行值。

标签: c# javascript jquery asp.net gridview


【解决方案1】:

要获取所选复选框的行值,您首先需要执行以下操作:在您的复选框控件中添加另外两个属性,如下所示

<asp:RadioButton ID="rbt" runat="server" OnCheckedChanged="rbt_CheckedChanged" 
                 AutoPostBack="true" onclick="javascript:Selrdbtn(this.id);" />

OnCheckedChanged 属性将在您的代码后面触发一个名为 rbt_CheckedChanged 的事件,AutoPostBack 将对您的控件执行操作。

然后您需要在 util 后面的代码中循环浏览您的网格视图,您可以在网格视图中找到所选值并同时获取控件的其余值。 p>

protected void rbt_CheckedChanged(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        if ((sender as CheckBox).ClientID == (row.FindControl("rbt") as CheckBox).ClientID)
        {
            string checkboxValue = (row.FindControl("rbt") as CheckBox).Checked.ToString();
            string textboxValue = (row.FindControl("TextBox1") as TextBox).Text;

            // you could get all of the selected row's values the same as above code.

            break; //break the loop once it finds the result
        }
    }
}

在上面的代码中,我放置了两个示例控件,向您展示如何获取所选行的值,以便您自己获取任何类型的控件的值。

干杯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多