您所在的位置: 程序员家园 -> 家园博客 ->
 
在哪里摔倒
就在哪里自己爬起来

用户登录

查  找

最新评论

最新留言

常用网站

网易邮箱 GMAIL  

百度搜索 MSDN

霏凡软件 BT精品

影视帝国 射 手 网

电驴下载 全 库 网

友情连接

茄菲的窝 冰冰博客

枫叶飘零 玫  瑰

ACEN 云 豹 子

统  计



在GridView里使用单选按钮,RadioButton/Radio
狼子 发表于 2007-5-27 12:59:00 阅读全文 | 回复(0) | 引用通告 | 编辑

在GridView里做单选按钮,我用了三种方法

第一种方法:在GridView的模版列里加服务器端控件RadioButton,使用js控制单选

<p>使用模版列里加RadioButton</p>
        <script type="text/javascript">
            function setRadio(nowRadio)
            {
                var myForm,objRadio;
                myForm=document.forms[0];
                ///alert(myForm);
                for(var i=0;i<myForm.length;i++)
                {
                    if(myForm.elements[i].type=="radio")
                    {
                        objRadio=myForm.elements[i];
                        ///alert(objRadio.name);
                        if(objRadio!=nowRadio && objRadio.name.indexOf("GridView1")>-1 && objRadio.name.indexOf("RadioButton1")>-1)
                        {
                        alert(objRadio.name);
                            if(objRadio.checked)
                            {
                                objRadio.checked=false;
                            }
                        }
                    }
                }
            }
        </script>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowHeader="False" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:RadioButton ID="RadioButton1" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Button ID="Button1" runat="server" Text="取选项" OnClick="Button1_Click" />
        <asp:Label ID="Label1" runat="server"></asp:Label>

蓝色那段就是控制单选的js,在这里,我使用了遍历页面上所有控件的方法,加入了条件,就是红色那个判断,只控制GridView1里id是RadioButton1生成的单选按钮

这种办法需要绑定客户端事件

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //给每个RadioButton1绑定setRadio事件
        try
        {
            ((RadioButton)e.Row.FindControl("RadioButton1")).Attributes.Add("onclick", "setRadio(this)");
        }
        catch (Exception)
        { }
    }

取值的方法就是遍历GridView的每一行,取选中的控件

protected void Button1_Click(object sender, EventArgs e)
    {
        //使用模版列里加RadioButton
        Label1.Text = "";
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            try
            {
                if (((RadioButton)gvr.FindControl("RadioButton1")).Checked)
                {
                    Label1.Text = "当前选中第" + Convert.ToString(gvr.RowIndex + 1) + "个";
                    break;
                }
            }
            catch (Exception)
            { }
        }
        if (Label1.Text.Length == 0)
        {
            Label1.Text = "没有选中项";
        }
    }

这种方法,在客户端和服务器端都使用了遍历

第二种方法:在GridView的模版列里,加html控件Radio

<p>使用模版列里加html控件Radio</p>
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" ShowHeader="False">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <input type="radio" name="myRadio" value='<%# Container.DataItemIndex.ToString() %>'>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Button ID="Button2" runat="server" Text="取选项" OnClick="Button2_Click" />
        <asp:Label ID="Label2" runat="server"></asp:Label>
        <script type="text/javascript">
            function setNowRadio(v)
            {
                //alert(v);
                var myForm,objRadio;
                myForm=document.forms[0];
                for(var i=0;i<myForm.length;i++)
                {
                    if(myForm.elements[i].type=="radio")
                    {
                        objRadio=myForm.elements[i];
                        //alert(objRadio.name);
                        //alert(objRadio.value);
                        if(objRadio.value==v)
                        {
                            objRadio.checked=true;
                        }
                    }
                }
            }
            <asp:Literal ID="jsLiteral" runat="server"></asp:Literal>
        </script>

就是蓝色那句,我在他的value值里,绑定的是当前行,因为一般在GridView里操作的时候,我们经常要用的是选中的行号,有了行号,我们就可以取GridView的DataKeys了

因为这里使用的是html控件,所以取数据的时候,要使用Request.Form

protected void Button2_Click(object sender, EventArgs e)
    {
        //使用模版列里加html控件Radio
        if (Request.Form["myRadio"] == null)
        {
            Label2.Text = "没有选中项";
            jsLiteral.Text = "";
        }
        else
        {
            string value;
            value = Request.Form["myRadio"].ToString();
            Label2.Text = "当前选中第" + Convert.ToString(Convert.ToInt16(value) + 1) + "个";
            jsLiteral.Text = "setNowRadio('" + value + "');";
        }
    }

这种方法自己,是不用遍历控件就可以完成任务的

就是因为使用的是客户端控件,所以选中的值不可以写入viewstate里面,如果有页面回传,这个值就不可以保留了,如果要在页面回传后还保留这个值,就要使用js,看红色那段代码,我选设置了一个setNowRadio(),然后呢加入Literal控件

在每一次回传的时候,嗯,因为我这里只有取值需要回传,所以我写在了取值那里,其实是应该写在Page_Load事件里的,加上if (IsPostBack)的判断,就是每次回传,就要取这个myRadio的值,执行函数,重新选择已经选中的项

在这个setNowRadio里,又用到了遍历,就是他比第一种方法遍历的东西少

第三种方法:直接使用RadioButtonList模拟表格

<p>使用RadioButtonList</p>
        <asp:RadioButtonList ID="RadioButtonList1" runat="server">
        </asp:RadioButtonList>
        <asp:Button ID="Button3" runat="server" Text="取选项" OnClick="Button3_Click" />
        <asp:Label ID="Label3" runat="server"></asp:Label>

我在这里模拟的是一个像论坛里,显示投票页面的东西,就是给出一个单选框,后面写选项内容,然后是一个图片,再显示有几票

private void SetListItem(RadioButtonList rbt)
    {
        //给RadioButtonList加几个ListItem,用来测试数据
        string item, space, info;
        int per;
        for (int i = 0; i < 3; i++)
        {
            per = 5;
            item = "<div style='float:left; width:300px;'> 第 " + Convert.ToString(i + 1) + " 项</div>";
            space = Convert.ToString(per * 3.50);
            space = "<div style='float:left; background-color:MistyRose;border-color:Silver;border-width:1px;border-style:solid; width:" + space + "px;'></div>";
            info = "<div style='float:left; width:70px;'>&nbsp;&nbsp;" + per.ToString() + "%&nbsp;&nbsp;5票</div>";
            info = item + space + info;
            RadioButtonList1.Items.Add(new ListItem(info, ""));
        }
    }

这种方法解决了单选的问题,解决了回传的问题,因为RadioButtonList本来就是生成一组Radio控件的,就是,在模拟的时候很麻烦,我这里使用了很多div+css,就是,我还是没有办法做到让生成的radio和选项放在同一行上

下面是生成的html代码里的一行:

<tr>
    <td>
        <input id="RadioButtonList1_0" type="radio" name="RadioButtonList1" value="" />
        <label for="RadioButtonList1_0">
            <div style='float:left; width:300px;'> 第 1 项</div>
            <div style='float:left; background-color:MistyRose;border-color:Silver;border-width:1px;border-style:solid; width:17.5px;'></div>
            <div style='float:left; width:70px;'>&nbsp;&nbsp;5%&nbsp;&nbsp;5票</div>
        </label>
    </td>
</tr>

div是块级元素,使用了float:left,也不可以让他们和radio在同一行上,如果可以把页面的宽度控制,比如确定是788px,那我们就可以使用float:right; text-align:left;来控制,就是很多时候,是不允许用px控制页面宽度的

----------------------------------------------------------------------------------------

对这两天的工作的总结,写了一个在论坛里:http://www.tiantiansoft.com/bbs/dispbbs.asp?boardid=40&id=152630

发表评论:

    昵称:
    密码:
    主页:
    标题:
Powered by Oblog.