TO feixr,DataGrid中的Radiobutton

來源:互聯網
上載者:User
datagrid 碰到的問題是如果用的是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 Form設計器所必需的。
            //
            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 Form設計器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <summary>
        /// 設計器支援所需的方法 - 不要使用代碼編輯器修改
        /// 此方法的內容。
        /// </summary>
        private void InitializeComponent()
        {    
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}

這種方法最大問題是必須每次Bind,暫時還沒想著好的解決辦法:(

相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。