工作中点滴记录

永远保持学徒心态

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

在Gridview模板中放置静态的控件Radio

//前台代码:

 <div style="text-align:center; font-size:smaller; margin:30px auto;">
        <asp:GridView ID="GridView1" BorderColor="Black" runat="server" AutoGenerateColumns="False" 
            onrowdatabound="GridView1_RowDataBound">
            <Columns>
            <asp:TemplateField HeaderText ="选择">
            <ItemTemplate>
               <%-- <asp:RadioButton ID="RadioButton1" GroupName="aa" runat="server" Text ='<%#Eval("CustomerID") %>' />--%>
                
               <input name="MyRadioButton" type="radio" value='<%#Eval("CustomerID") %>' />
            </ItemTemplate>
            </asp:TemplateField>
                <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" />
                <asp:BoundField DataField="ContactName" HeaderText="ContactName" />
                <asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" />
            </Columns>
            <HeaderStyle BackColor="Azure" Font-Size="12px" HorizontalAlign="Center" />
            <RowStyle HorizontalAlign="Left" />
        </asp:GridView>
    </div>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
 

后台代码:

  protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            databind();
        }
    }
    public void databind()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
        SqlCommand cmd = new SqlCommand("SELECT top 10 * FROM CUSTOMERS", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        this.GridView1.DataSource = ds.Tables[0];
        this.GridView1.DataKeyNames = new string[] { "CustomerID" };
        this.GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        foreach (TableCell  item in e.Row.Cells)
        {
            item.Attributes.Add("style", "border-color:black");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write(Request.Form["MyRadioButton"].ToString());
    }
截图如下:

posted on 2011-03-13 10:50  梦里故乡  阅读(605)  评论(0编辑  收藏  举报