一个GridView内从单选按钮列表ASP读值 [英] ASP Read value from RadioButtonList inside of a GridView

查看:139
本文介绍了一个GridView内从单选按钮列表ASP读值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET的GridView至极我填写的数据,一键,和一个RadioButtonList的4个单选按钮。我怎样才能至极单选按钮被pressing外的按钮在GridView的选择(使用C#codebehind)?
GridView控件内的按钮应当用于移除一行(RowCommand事件,我认为...)
谢谢!

在GridView中code:

 <柱体和GT;
    < ASP:BoundField的数据字段=名称的HeaderText =名称/>
    < ASP:BoundField的数据字段=值的HeaderText =值/>
    < ASP:的TemplateField ShowHeader =假的HeaderText =?富>
        <&ItemTemplate中GT;
            < ASP:RadioButtonList的ID =RadioButtonList1=服务器RepeatDirection =水平>
                < ASP:列表项选择=真正的>项目1 LT; / ASP:ListItem的>
                < ASP:ListItem的>项目1 LT; / ASP:ListItem的>
                < ASP:ListItem的>项目2'; / ASP:ListItem的>
                < ASP:ListItem的>项目3'; / ASP:ListItem的>
                < ASP:ListItem的>项目4℃; / ASP:ListItem的>
            < / ASP:RadioButtonList的>
        < / ItemTemplate中>
    < / ASP:的TemplateField>
    < ASP:的TemplateField ShowHeader =假的HeaderText =>
        <&ItemTemplate中GT;
            < ASP:按钮的ID =Button1的=服务器文本=删除/>
        < / ItemTemplate中>
    < / ASP:的TemplateField>
< /专栏>


解决方案

要知道哪个单选按钮被选中请从目前的code下列步骤操作:

修改您的按钮,这样的:

 < ASP:的TemplateField ShowHeader =假的HeaderText =>
    <&ItemTemplate中GT;
        < ASP:按钮的ID =Button1的=服务器文本=删除CommandArgument =<%#((GridViewRow)集装箱).RowIndex%GT;
            的CommandName =删除/>
    < / ItemTemplate中>
< / ASP:的TemplateField>

所以,现在你有的CommandName CommandArgument 属性填写。在 CommandArgument 将行的索引传递给你的 RowCommand 事件。

那么你的 RowCommand 事件是这样的:

 保护无效GridView1_RowCommand(对象发件人,GridViewCommandEventArgs E)
{
    如果(e.CommandName ==删除)
    {
        INT指数= Convert.ToInt32(e.CommandArgument);
        如果(索引> = 0)
        {
            //索引是行,现在得到RadioButtonList1此行中
            单选按钮列表RBL =(单选按钮列表)GridView1.Rows [指数] .FindControl(RadioButtonList1);
            如果(RBL!= NULL)
            {
                选择= rbl.SelectedItem.Text串;
                的Response.Write(行+指数+被选中,单选按钮+选择);
            }
        }
    }
}

注意:我会建议增加你的单选按钮让你检查对值,而不是文字。

I have a ASP.NET GridView wich I fill with data, one button, and one RadioButtonList with 4 radio buttons. How can I get wich radio button is selected by pressing a button outside of the GridView (using c# codebehind)? The button inside the GridView shall be used for removing a row (RowCommand event I think...) Thanks!

Code from within the GridView:

<Columns>
    <asp:BoundField DataField="name" HeaderText="Name" />
    <asp:BoundField DataField="value" HeaderText="Value" />
    <asp:TemplateField ShowHeader="false" HeaderText="Foo?">
        <ItemTemplate>
            <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
                <asp:ListItem Selected="true">Item 1</asp:ListItem>
                <asp:ListItem>Item 1</asp:ListItem>
                <asp:ListItem>Item 2</asp:ListItem>
                <asp:ListItem>Item 3</asp:ListItem>
                <asp:ListItem>Item 4</asp:ListItem>
            </asp:RadioButtonList>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField ShowHeader="false" HeaderText="">
        <ItemTemplate>
            <asp:Button ID="Button1" runat="server" Text="Remove" />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

解决方案

To know which RadioButton was selected follow these steps from your current code:

Modify your button to this:

<asp:TemplateField ShowHeader="false" HeaderText="">
    <ItemTemplate>
        <asp:Button ID="Button1" runat="server" Text="Remove" CommandArgument="<%#  ((GridViewRow) Container).RowIndex%>"
            CommandName="remove" />
    </ItemTemplate>
</asp:TemplateField>

so now you have CommandName and CommandArgument properties filled in. The CommandArgument will pass the index of the row to your RowCommand event.

Then your RowCommand event looks like this:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "remove")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        if (index >= 0)
        { 
            //index is the row, now obtain the RadioButtonList1 in this row
            RadioButtonList rbl = (RadioButtonList)GridView1.Rows[index].FindControl("RadioButtonList1");
            if (rbl != null)
            {
                string selected = rbl.SelectedItem.Text;
                Response.Write("Row " + index + " was selected, radio button " + selected);
            }
        }
    }
}

Note: I would recommend adding Value to your RadioButtons so that you check against values and not text.

这篇关于一个GridView内从单选按钮列表ASP读值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆