首页 > 开发 > 综合 > 正文

TO feixr,DataGrid中的Radiobutton

2024-07-21 02:22:01
字体:
来源:转载
供稿:网友

商业源码热门下载www.html.org.cn

碰到的问题是如果用的是server端的radio控件的话
系统会自动的给它分配name以相互区分
这样就破坏了我们单选的目的
但如果用一般的htmlcontrol,又不能保存状态
所以我自己写了一个用户控件
给你参考一下,代码如下:
userradio.ascx.cs

namespace examwebui
{
    using system;
    using system.data;
    using system.drawing;
    using system.web;
    using system.web.ui;
    using system.web.ui.webcontrols;
    using system.web.ui.htmlcontrols;
    using system.collections.specialized;  

    /// <summary>
    ///        webusercontrol1 的摘要说明。
    /// </summary>
    public abstract class userradio : system.web.ui.usercontrol
    {
        private string rvalue;
        private string rname;
        private string fvalue;
        private bool postcheck;

        private void page_load(object sender, system.eventargs e)
        {
            fvalue=this.page.request.form[this.name];
            if(this.rvalue==this.fvalue)
                postcheck=true;
        }

        protected override void render(htmltextwriter output)
        {
            string outstr;
            if(postcheck)
                outstr="<h3>value: <input name=" + this.name + " type=radio value=" + this.value + " checked> </h3>";
            else
                outstr="<h3>value: <input name=" + this.name + " type=radio value=" + this.value + " > </h3>";
            output.write(outstr);
        }

        public string value
        {
            get
            {
                return this.rvalue;
            }
            set
            {
                this.rvalue = value;                
            }
        }

        public string name
        {
            get
            {
                return this.rname;
            }
            set
            {
                this.rname = value;
            }
        }


        #region web form designer generated code
        override protected void oninit(eventargs e)
        {
            //
            // codegen:该调用是 asp.net web 窗体设计器所必需的。
            //
            initializecomponent();
            base.oninit(e);
        }
        
        ///        设计器支持所需的方法 - 不要使用
        ///        代码编辑器修改此方法的内容。
        /// </summary>
        private void initializecomponent()
        {
            this.load += new system.eventhandler(this.page_load);
        }
        #endregion
    }
}
userradio.ascx

<%@ control language="c#" autoeventwireup="false" codebehind="userradio.ascx.cs" inherits="examwebui.userradio" targetschema="http://schemas.microsoft.com/intellisense/ie5"%>

使用该控件的例子

test1.aspx

<%@ page language="c#" codebehind="test1.aspx.cs" autoeventwireup="false" inherits="examwebui.test1" %>
<%@ register tagprefix="dxuc" tagname="radio" src="userradio.ascx" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
    <head>
        <title>test1</title>
        <meta name="generator" content="microsoft visual studio 7.0">
        <meta name="code_language" content="c#">
        <meta name="vs_defaultclientscript" content="javascript">
        <meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
    </head>
    <body ms_positioning="gridlayout">
        <form id="test1" method="post" runat="server">
            <font face="宋体">
                <asp:datagrid id="datagrid1" style="z-index: 101; left: 230px; position: absolute; top: 136px" runat="server" width="221px" height="189px">
                    <columns>
                        <asp:templatecolumn>
                        <itemtemplate>
                    <dxuc:radio id="rd1" runat=server value=<%#container.dataitem%> name="hahah"></dxuc:radio>
                        </itemtemplate>     
                        </asp:templatecolumn>
                    </columns>
                </asp:datagrid>                    
                <asp:button id=s1 runat=server></asp:button>   
                </font>
        </form>
    </body>
</html>

test1.aspx.cs

using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;

namespace examwebui
{
    /// <summary>
    /// test1 的摘要说明。
    /// </summary>
    public class test1 : system.web.ui.page
    {
        protected system.web.ui.webcontrols.datagrid datagrid1;
    
        private void page_load(object sender, system.eventargs e)
        {
            system.collections.arraylist a=new system.collections.arraylist();
            a.add("a");
            a.add("b");
            a.add("c");

            this.datagrid1.datasource=a;
            this.datagrid1.databind();
        }

        #region web form designer generated code
        override protected void oninit(eventargs e)
        {
            //
            // codegen:该调用是 asp.net web 窗体设计器所必需的。
            //
            initializecomponent();
            base.oninit(e);
        }
        
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void initializecomponent()
        {    
            this.load += new system.eventhandler(this.page_load);

        }
        #endregion
    }
}

这种方法最大问题是必须每次bind,暂时还没想着好的解决办法:(
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表