程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> gridview中實現radiobutton的單選

gridview中實現radiobutton的單選

編輯:C#入門知識

c# 代碼[csharp]
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
        { 
            if (e.Row.RowType == DataControlRowType.DataRow) 
            { 
                RadioButton rb = (RadioButton)e.Row.FindControl("rbtSelect"); 
                if (rb != null) 
                    rb.Attributes.Add("onclick", "onClientClick('" + rb.ClientID + "','" + e.Row.RowIndex + "')");  //把選中行的RowIndex也傳過去,提交後在服務器端取值時用  
            } 
        }      

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                RadioButton rb = (RadioButton)e.Row.FindControl("rbtSelect");
                if (rb != null)
                    rb.Attributes.Add("onclick", "onClientClick('" + rb.ClientID + "','" + e.Row.RowIndex + "')");  //把選中行的RowIndex也傳過去,提交後在服務器端取值時用
            }
        }    
javascript代碼
[javascript]
<script type="text/javascript"> 
       function onClientClick(selectedId, rowIndex) 
       { 
            //用隱藏控件記錄下選中的行號  
            var hidden = document.getElementById("Hidden1").value=rowIndex; 
             
            var inputs = document.getElementById("<%=GridView1.ClientID%>").getElementsByTagName("input"); 
            for(var i=0; i <inputs.length; i++) 
            { 
                if(inputs[i].type=="radio") 
                { 
                    if(inputs[i].id==selectedId) 
                        inputs[i].checked = true; 
                    else 
                        inputs[i].checked = false; 
                    
                } 
            } 
       } 
     </script> 

<script type="text/javascript">
       function onClientClick(selectedId, rowIndex)
       {
            //用隱藏控件記錄下選中的行號
            var hidden = document.getElementById("Hidden1").value=rowIndex;
           
            var inputs = document.getElementById("<%=GridView1.ClientID%>").getElementsByTagName("input");
            for(var i=0; i <inputs.length; i++)
            {
                if(inputs[i].type=="radio")
                {
                    if(inputs[i].id==selectedId)
                        inputs[i].checked = true;
                    else
                        inputs[i].checked = false;
                  
                }
            }
       }
     </script>

hmtl代碼:
[html]
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  
                    Width="648px" Font-Size="9pt" onrowcommand="GridView1_RowCommand"  
                    DataKeyNames="id" onrowdatabound="GridView1_RowDataBound"> 
                    <Columns>      
                    <asp:TemplateField> 
                    <ItemTemplate>                     
                        <asp:RadioButton ID="rbtSelect" runat="server" />                     
                    </ItemTemplate> 
                    </asp:TemplateField>                    
                        <asp:TemplateField HeaderText="文件名">                             
                            <ItemTemplate> 
                            <asp:LinkButton runat="server" ID="lbtDirName" CommandName="Change" CommandArgument='<%#Container.DataItemIndex %>'> 
                            <%#Eval("AA") %> 
                            </asp:LinkButton>                                 
                            </ItemTemplate> 
                        </asp:TemplateField> 
                        <asp:BoundField DataField="BB" HeaderText="字段1" /> 
                        <asp:BoundField DataField="CC" HeaderText="字段2" /> 
                        <asp:BoundField DataField="DD" HeaderText="字段3" /> 
                        <asp:BoundField DataField="EE" HeaderText="字段4" /> 
                    </Columns> 
                </asp:GridView> 
 
                 <input id="Hidden1" type="hidden" runat="server"/> 

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                    Width="648px" Font-Size="9pt" onrowcommand="GridView1_RowCommand"
                    DataKeyNames="id" onrowdatabound="GridView1_RowDataBound">
                    <Columns>    
                    <asp:TemplateField>
                    <ItemTemplate>                   
                        <asp:RadioButton ID="rbtSelect" runat="server" />                   
                    </ItemTemplate>
                    </asp:TemplateField>                  
                        <asp:TemplateField HeaderText="文件名">                           
                            <ItemTemplate>
                            <asp:LinkButton runat="server" ID="lbtDirName" CommandName="Change" CommandArgument='<%#Container.DataItemIndex %>'>
                            <%#Eval("AA") %>
                            </asp:LinkButton>                               
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="BB" HeaderText="字段1" />
                        <asp:BoundField DataField="CC" HeaderText="字段2" />
                        <asp:BoundField DataField="DD" HeaderText="字段3" />
                        <asp:BoundField DataField="EE" HeaderText="字段4" />
                    </Columns>
                </asp:GridView>

                 <input id="Hidden1" type="hidden" runat="server"/>


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved