RadioButton加入DataGrid模板列引起的问题。
生活随笔
收集整理的這篇文章主要介紹了
RadioButton加入DataGrid模板列引起的问题。
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
也許各位看官還尚未注意到,將RadioButton加入到DataGrid模板列后(當(dāng)然DataList,Repeater也一樣),盡管你設(shè)置了其GroupName,結(jié)果還是不能實(shí)現(xiàn)我們想要的效果, 即實(shí)現(xiàn)RadioButton之初衷:實(shí)現(xiàn)單選效果。
也許,你會(huì)說,用RadioButtonList即可解決,如:
?1?<asp:datagrid?id="DataGrid1"?runat="server"?
?2?//1、我們將datagrid的第一列設(shè)置為模板列,并加入RadioButtonList?
?3?AutoGenerateColumns="False">
?4?<Columns>??????????????????????<asp:TemplateColumn>
?5?????<ItemTemplate>????????????????????<asp:RadioButtonList?ID="RadioButtonList1"?Runat="server"></asp:RadioButtonList>
?6??????</ItemTemplate>
?7???</asp:TemplateColumn>????????????????<asp:BoundColumn?DataField="CustomerID"?HeaderText="CustomerID"></asp:BoundColumn>
?8?</Columns>
?9?</asp:datagrid>
10?//然后在數(shù)據(jù)綁定到DataGrid后,即DataGrid1.DataBind();代碼之后,寫:
11?//將第一列第一單元格的RowSpan設(shè)置為DataGrid的總列數(shù)
12?DataGrid.Items[0].Cells[0].RowSpan=DataGrid.Items.Count;??
13?for?(int?i=1;i<DataGrid.Items.Count;++i)
14?{??????????????DataGrid.Items[i].Cells[0].Visible=false;??
15?//從第二列開始隱藏第一個(gè)單元格
16?}
17?//將第一列第一個(gè)單元格里的RadioButtonList按照DataGrid的總列數(shù)進(jìn)行列添加
18?for?(int?i=0;i<DataGrid.Items.Count;++i)
19?{
20?ListItem?li=new?ListItem("","1");
21?((RadioButtonList)DataGrid1.Items[0].Cells[0].Controls[1]).Items.Add(li);
22?}
23?而確定哪項(xiàng)被選中,可通過?DataGrid1.DataKeys[((RadioButtonList)DataGrid1.Items[0].Cells[0].Controls[1]).SelectedIndex]?
24?得到。 當(dāng)然,最簡(jiǎn)單的方法就是使用<input type="radio" name=“radioDemo">HTML控件,使用簡(jiǎn)單,就不再重復(fù)了。
而如果我們想用RadioButton實(shí)現(xiàn)呢?
這時(shí)候,我們就必須思考為什么
<input type="radio" name="radioDemo">可以實(shí)現(xiàn)單選
而
<input type="radio" name="radioDemo" id="radioDemo" runat="server">
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="Demo" />
卻不能實(shí)現(xiàn)單選呢?
如果我們查看瀏覽器輸出的源代碼,會(huì)發(fā)現(xiàn)
Runat="server"的RadioButton的Name變成了類似
1?<input?id="countriesGrid__ctl2_selectRadioButton"??type="radio"?name="countriesGrid:_ctl2:country"?value="selectRadioButton"?/>這樣的Name了,故其實(shí)現(xiàn)不了單選。
所以,我們只有讓DataGrid輸出唯一的ClientID
以下為實(shí)現(xiàn)代碼:
??1?using?System;
??2?using?System.Web.UI;
??3?using?System.Web.UI.WebControls;
??4?using?System.Globalization;
??5?
??6?namespace?Renyu.Web.UI.WebControls
??7?{
??8?????[ToolboxData("<{0}:GroupRadioButton?runat=server></{0}:GroupRadioButton>")]
??9?????public?class?GroupRadioButton?:?RadioButton,?IPostBackDataHandler
?10?????{
?11?????????public?GroupRadioButton()?:?base()
?12?????????{
?13?????????}
?14?
?15?????????#region?Properties
?16?
?17?????????private?string?Value
?18?????????{
?19?????????????get
?20?????????????{
?21?????????????????string?val?=?Attributes["value"];
?22?????????????????if(val?==?null)
?23?????????????????????val?=?UniqueID;
?24?????????????????else
?25?????????????????????val?=?UniqueID?+?"_"?+?val;
?26?????????????????return?val;
?27?????????????}
?28?????????}
?29?
?30?????????#endregion
?31?????????
?32?????????#region?Rendering
?33?
?34?????????protected?override?void?Render(HtmlTextWriter?output)
?35?????????{
?36?????????????RenderInputTag(output);
?37?????????}
?38?
?39?????????private?void?RenderInputTag(HtmlTextWriter?htw)
?40?????????{
?41?????????????htw.AddAttribute(HtmlTextWriterAttribute.Id,?ClientID);
?42?????????????htw.AddAttribute(HtmlTextWriterAttribute.Type,?"radio");
?43?????????????htw.AddAttribute(HtmlTextWriterAttribute.Name,?GroupName);
?44?????????????htw.AddAttribute(HtmlTextWriterAttribute.Value,?Value);
?45?????????????if(Checked)
?46?????????????????htw.AddAttribute(HtmlTextWriterAttribute.Checked,?"checked");
?47?????????????if(!Enabled)
?48?????????????????htw.AddAttribute(HtmlTextWriterAttribute.Disabled,?"disabled");
?49?????????????
?50?????????????string?onClick?=?Attributes["onclick"];
?51?????????????if(AutoPostBack)
?52?????????????{
?53?????????????????if(onClick?!=?null)
?54?????????????????????onClick?=?String.Empty;
?55?????????????????onClick?+=?Page.GetPostBackClientEvent(this,?String.Empty);
?56?????????????????htw.AddAttribute(HtmlTextWriterAttribute.Onclick,?onClick);
?57?????????????????htw.AddAttribute("language",?"javascript");
?58?????????????}
?59?????????????else
?60?????????????{
?61?????????????????if(onClick?!=?null)
?62?????????????????????htw.AddAttribute(HtmlTextWriterAttribute.Onclick,?onClick);
?63?????????????}
?64?
?65?????????????if(AccessKey.Length?>?0)
?66?????????????????htw.AddAttribute(HtmlTextWriterAttribute.Accesskey,?AccessKey);
?67?????????????if(TabIndex?!=?0)
?68?????????????????htw.AddAttribute(HtmlTextWriterAttribute.Tabindex,?
?69?????????????????????TabIndex.ToString(NumberFormatInfo.InvariantInfo));
?70?????????????htw.RenderBeginTag(HtmlTextWriterTag.Input);
?71?????????????htw.RenderEndTag();
?72?????????}
?73?
?74?????????#endregion
?75?
?76?????????#region?IPostBackDataHandler?Members
?77?
?78?????????void?IPostBackDataHandler.RaisePostDataChangedEvent()
?79?????????{
?80?????????????OnCheckedChanged(EventArgs.Empty);
?81?????????}
?82?
?83?????????bool?IPostBackDataHandler.LoadPostData(string?postDataKey,?
?84?????????????System.Collections.Specialized.NameValueCollection?postCollection)
?85?????????{
?86?????????????bool?result?=?false;
?87?????????????string?value?=?postCollection[GroupName];
?88?????????????if((value?!=?null)?&&?(value?==?Value))
?89?????????????{
?90?????????????????if(!Checked)
?91?????????????????{
?92?????????????????????Checked?=?true;
?93?????????????????????result?=?true;
?94?????????????????}
?95?????????????}
?96?????????????else
?97?????????????{
?98?????????????????if(Checked)
?99?????????????????????Checked?=?false;
100?????????????}
101?????????????return?result;
102?????????}
103?
104?????????#endregion
105?????}
106?}
107?編譯為.dll后,即可像System.Web.UI.WebControls.RadioButton一樣使用了。
也許,你會(huì)說,用RadioButtonList即可解決,如:
?1?<asp:datagrid?id="DataGrid1"?runat="server"?
?2?//1、我們將datagrid的第一列設(shè)置為模板列,并加入RadioButtonList?
?3?AutoGenerateColumns="False">
?4?<Columns>??????????????????????<asp:TemplateColumn>
?5?????<ItemTemplate>????????????????????<asp:RadioButtonList?ID="RadioButtonList1"?Runat="server"></asp:RadioButtonList>
?6??????</ItemTemplate>
?7???</asp:TemplateColumn>????????????????<asp:BoundColumn?DataField="CustomerID"?HeaderText="CustomerID"></asp:BoundColumn>
?8?</Columns>
?9?</asp:datagrid>
10?//然后在數(shù)據(jù)綁定到DataGrid后,即DataGrid1.DataBind();代碼之后,寫:
11?//將第一列第一單元格的RowSpan設(shè)置為DataGrid的總列數(shù)
12?DataGrid.Items[0].Cells[0].RowSpan=DataGrid.Items.Count;??
13?for?(int?i=1;i<DataGrid.Items.Count;++i)
14?{??????????????DataGrid.Items[i].Cells[0].Visible=false;??
15?//從第二列開始隱藏第一個(gè)單元格
16?}
17?//將第一列第一個(gè)單元格里的RadioButtonList按照DataGrid的總列數(shù)進(jìn)行列添加
18?for?(int?i=0;i<DataGrid.Items.Count;++i)
19?{
20?ListItem?li=new?ListItem("","1");
21?((RadioButtonList)DataGrid1.Items[0].Cells[0].Controls[1]).Items.Add(li);
22?}
23?而確定哪項(xiàng)被選中,可通過?DataGrid1.DataKeys[((RadioButtonList)DataGrid1.Items[0].Cells[0].Controls[1]).SelectedIndex]?
24?得到。 當(dāng)然,最簡(jiǎn)單的方法就是使用<input type="radio" name=“radioDemo">HTML控件,使用簡(jiǎn)單,就不再重復(fù)了。
而如果我們想用RadioButton實(shí)現(xiàn)呢?
這時(shí)候,我們就必須思考為什么
<input type="radio" name="radioDemo">可以實(shí)現(xiàn)單選
而
<input type="radio" name="radioDemo" id="radioDemo" runat="server">
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="Demo" />
卻不能實(shí)現(xiàn)單選呢?
如果我們查看瀏覽器輸出的源代碼,會(huì)發(fā)現(xiàn)
Runat="server"的RadioButton的Name變成了類似
1?<input?id="countriesGrid__ctl2_selectRadioButton"??type="radio"?name="countriesGrid:_ctl2:country"?value="selectRadioButton"?/>這樣的Name了,故其實(shí)現(xiàn)不了單選。
所以,我們只有讓DataGrid輸出唯一的ClientID
以下為實(shí)現(xiàn)代碼:
??1?using?System;
??2?using?System.Web.UI;
??3?using?System.Web.UI.WebControls;
??4?using?System.Globalization;
??5?
??6?namespace?Renyu.Web.UI.WebControls
??7?{
??8?????[ToolboxData("<{0}:GroupRadioButton?runat=server></{0}:GroupRadioButton>")]
??9?????public?class?GroupRadioButton?:?RadioButton,?IPostBackDataHandler
?10?????{
?11?????????public?GroupRadioButton()?:?base()
?12?????????{
?13?????????}
?14?
?15?????????#region?Properties
?16?
?17?????????private?string?Value
?18?????????{
?19?????????????get
?20?????????????{
?21?????????????????string?val?=?Attributes["value"];
?22?????????????????if(val?==?null)
?23?????????????????????val?=?UniqueID;
?24?????????????????else
?25?????????????????????val?=?UniqueID?+?"_"?+?val;
?26?????????????????return?val;
?27?????????????}
?28?????????}
?29?
?30?????????#endregion
?31?????????
?32?????????#region?Rendering
?33?
?34?????????protected?override?void?Render(HtmlTextWriter?output)
?35?????????{
?36?????????????RenderInputTag(output);
?37?????????}
?38?
?39?????????private?void?RenderInputTag(HtmlTextWriter?htw)
?40?????????{
?41?????????????htw.AddAttribute(HtmlTextWriterAttribute.Id,?ClientID);
?42?????????????htw.AddAttribute(HtmlTextWriterAttribute.Type,?"radio");
?43?????????????htw.AddAttribute(HtmlTextWriterAttribute.Name,?GroupName);
?44?????????????htw.AddAttribute(HtmlTextWriterAttribute.Value,?Value);
?45?????????????if(Checked)
?46?????????????????htw.AddAttribute(HtmlTextWriterAttribute.Checked,?"checked");
?47?????????????if(!Enabled)
?48?????????????????htw.AddAttribute(HtmlTextWriterAttribute.Disabled,?"disabled");
?49?????????????
?50?????????????string?onClick?=?Attributes["onclick"];
?51?????????????if(AutoPostBack)
?52?????????????{
?53?????????????????if(onClick?!=?null)
?54?????????????????????onClick?=?String.Empty;
?55?????????????????onClick?+=?Page.GetPostBackClientEvent(this,?String.Empty);
?56?????????????????htw.AddAttribute(HtmlTextWriterAttribute.Onclick,?onClick);
?57?????????????????htw.AddAttribute("language",?"javascript");
?58?????????????}
?59?????????????else
?60?????????????{
?61?????????????????if(onClick?!=?null)
?62?????????????????????htw.AddAttribute(HtmlTextWriterAttribute.Onclick,?onClick);
?63?????????????}
?64?
?65?????????????if(AccessKey.Length?>?0)
?66?????????????????htw.AddAttribute(HtmlTextWriterAttribute.Accesskey,?AccessKey);
?67?????????????if(TabIndex?!=?0)
?68?????????????????htw.AddAttribute(HtmlTextWriterAttribute.Tabindex,?
?69?????????????????????TabIndex.ToString(NumberFormatInfo.InvariantInfo));
?70?????????????htw.RenderBeginTag(HtmlTextWriterTag.Input);
?71?????????????htw.RenderEndTag();
?72?????????}
?73?
?74?????????#endregion
?75?
?76?????????#region?IPostBackDataHandler?Members
?77?
?78?????????void?IPostBackDataHandler.RaisePostDataChangedEvent()
?79?????????{
?80?????????????OnCheckedChanged(EventArgs.Empty);
?81?????????}
?82?
?83?????????bool?IPostBackDataHandler.LoadPostData(string?postDataKey,?
?84?????????????System.Collections.Specialized.NameValueCollection?postCollection)
?85?????????{
?86?????????????bool?result?=?false;
?87?????????????string?value?=?postCollection[GroupName];
?88?????????????if((value?!=?null)?&&?(value?==?Value))
?89?????????????{
?90?????????????????if(!Checked)
?91?????????????????{
?92?????????????????????Checked?=?true;
?93?????????????????????result?=?true;
?94?????????????????}
?95?????????????}
?96?????????????else
?97?????????????{
?98?????????????????if(Checked)
?99?????????????????????Checked?=?false;
100?????????????}
101?????????????return?result;
102?????????}
103?
104?????????#endregion
105?????}
106?}
107?編譯為.dll后,即可像System.Web.UI.WebControls.RadioButton一樣使用了。
總結(jié)
以上是生活随笔為你收集整理的RadioButton加入DataGrid模板列引起的问题。的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: XML数据的分页显示
- 下一篇: 找到一篇有关A*算法文章,不错~收藏