如何在使用asp.net中的数据列表选中复选框时获取列数据

时间:2015-07-26 08:00:02

标签: c# asp.net

这是eswar.k,我在asp.net中有一个问题..那就是..

我有一个datalist。这是显示来自数据库的数据..那是包含.check框,图像和标签..有什么问题..当我勾选复选框时,我必须显示电子邮件标签放入文本框..(如多个收件人,例如:eswar @ gmil.com,eee @ yahoo.in..etc)

protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        string strconnstring = System.Configuration.ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
        string strquery = "select chid,chname,chlanguage,chrating,chemail,contenttype,data from tbl_channel_join Order by chid";
        SqlCommand cmd = new SqlCommand(strquery);
        SqlConnection con = new SqlConnection(strconnstring);
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;

        try        
        {
            con.Open();
            sda.SelectCommand = cmd;
            sda.Fill(dt);
            //GridView1.DataSource = dt;
            //GridView1.DataBind();
            //GridView2.DataSource = dt;
            //GridView2.DataBind();
            dl_channels.DataSource = dt;
            dl_channels.DataBind();

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            con.Close();
            sda.Dispose();
            con.Dispose();
            dt.Dispose();
        }

1 个答案:

答案 0 :(得分:0)

我们假设你有一个Gridview,其复选框如下:

 <asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="checkIT" runat="server"/>
                </ItemTemplate>
            </asp:TemplateField>
    </asp:GridView>

 <asp:Button ID="btnDisplay" runat="server" Text="Show data selected"  OnClick="btnDisplay_Click"/>    

<asp:TextBox id="textboxDataDisplay" runat="server" />

带一个按钮,显示所选的复选框列

C#代码

protected void btnDisplay_Click(object sender, EventArgs e)
{
    string data = "";

    foreach (GridViewRow row in GridView1.Rows)
    {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkRow = (row.Cells[0].FindControl("chkCtrl") as CheckBox);
                if (chkRow.Checked)
                {
                    string yourFirstRowCell = row.Cells[1].Text;
                    string yourSecondRowCell = row.Cells[2].Text;
                    string yourThirdRowCell = row.Cells[3].Text;
                    data = yourFirstRowCell + yourSecondRowCell + yourThirdRowCell; 
                }
            }
    }

    textboxDataDisplay.text = data; 
}

行单元格是您要获取复选框所在行的单元格。