当前位置:编程学习 > C#/ASP.NET >>

急问题,在线等

textbox.DataBindings.Add("Text", ds.Table[0], "Col1");
这样可以绑定textbox的Text到列

我现在有个CheckBox要绑定到列,这个列的值分别为"1"和"2"
要求等于"1"时CheckBox选定,"2"时不选定,如何做啊?
--------------------编程问答-------------------- HTML:
------------------------------------------------------
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="161px" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"IDvalue") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="aa" HeaderText="aa" />
            </Columns>
        </asp:GridView>
    </form>
</body>
</html>


C#:
---------------------------------------------------------------
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();    
        }
    }

    public void Bind()
    {
        SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=;database=Demo");
        SqlDataAdapter da = new SqlDataAdapter("select * from zy",con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chk = (CheckBox)e.Row.FindControl("CheckBox1");
            if (chk.Text == "1")
            {
                chk.Checked = true;
            }
            else if (chk.Text == "2")
            {
                chk.Checked = false;
            }
        }
    } --------------------编程问答-------------------- 表结构:
------------------------------------------------------
IDvalue        aa

   1           aa
   2           aa
   1           aa
   1           aa
   2           aa
补充:.NET技术 ,  .NET Framework
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,