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

来源:http://blog.tiantiansoft.com/user1/9/archives/2007/3841.html

第一种方法:在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控制页面宽度的

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,我作为一个语言模型并不能提供完整的代码和XML控件界面。不过,我可以提供一些关于安卓开发的基本知识和建议,帮助你更好地学习和开发。 首先,安卓开发主要使用Java语言和XML布局文件。要实现文本标签、偏移框、图像、提示框等基本控件,可以使用Android SDK提供的各种控件类。例如,TextView控件用于显示文本,Button控件用于创建按钮,ImageView控件用于显示图像,ProgressBar控件用于显示进度条等等。这些控件都可以在XML布局文件中进行配置。 对于单选按钮和复选按钮,可以使用RadioButton和CheckBox控件。ProgressBar控件可以设置为水平或圆形样式。列表可以使用ListView、RecyclerView或GridView控件。菜单可以使用OptionsMenu或PopupWindow控件。对话框可以使用AlertDialog或DialogFragment控件。 图形可以使用Canvas和Paint类进行绘制。动画可以使用Animation和Animator类进行创建。音频可以使用MediaPlayer或SoundPool类进行播放。视频可以使用VideoView控件进行播放。 传感器可以使用SensorManager类进行管理。通知可以使用NotificationManager类进行创建和管理。简单存储可以使用SharedPreferences类进行实现,文件存储可以使用File类进行读写,数据库存储可以使用SQLiteOpenHelper类进行管理,Webview可以使用WebView控件进行显示。 总的来说,安卓开发需要掌握Java语言和XML布局,以及Android SDK提供的各种控件和类库。建议可以先学习一些基础知识,然后通过实践逐步掌握更多的技能。同时,可以参考官方文档和一些优秀的开源项目进行学习和实践。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值