我需要在GridView中使用Checkboxlist来绑定来自其他表的数据

问题描述 投票:0回答:4

我必须填写gridview控件中的checkboxlist。

<asp:GridView ID="gvProject" CssClass="Gridview viewproject-th-col-remov" runat="server"    AutoGenerateColumns="false" OnRowDataBound="gvProject_RowDataBound" DataKeyNames="ProjectID" AllowPaging="True">
<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton ID="lnkContract" runat="server" Text="Contract" ControlStyle-CssClass="pdf-download-link"OnClick="DownloadFile" CommandArgument='<%# Eval("ProjectId") %>'></asp:LinkButton>
            <asp:CheckBoxList ID="chkAdSpace" runat="server"></asp:CheckBoxList>
        </ItemTemplate>
    </asp:TemplateField>
</Columns> </asp:GridView>

然后我像这样绑定了checkboxlist中的数据。

private void BindList(Int32 _id)
{
    DataTable dtAdSpace = new DataTable();
    dtAdSpace = CommonDataViews.GetAdSpaceSearch(_id);
    if (dtAdSpace.Rows.Count > 0)
    {
        chkAdSpace.DataSource = dtAdSpace;
        chkAdSpace.DataTextField = "adspace";
        chkAdSpace.DataValueField = "adspace";
        chkAdSpace.DataBind();
    }
}

但是,问题是chkASpace无法访问int BindList(Int32 _id)或通过表单。请帮助我为什么这不能像我使用的LinkBut​​ton那样访问。

提前致谢。

asp.net gridview
4个回答
0
投票

要检查列表中的所有复选框只是遍历每个复选框以检查它们。在Databind之后。

foreach (ListItem listItem in chkAdSpace.Items)
{
     listItem.Selected = true;
}

1
投票

检索要绑定在DataTable中的数据。在GridView_RowDataBound事件中,使用DataTable绑定Checkboxlist。使用FindControl获取Checkboxlist。如下所示:

protected void gvProject_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
       CheckBoxList cblProject = (CheckBoxList)e.Row.FindControl("chkAdSpace");
       cblProject.DataSource = dtAdSpace;
       cblProject.DataTextField = "adspace";
       cblProject.DataValueField = "adspace";
       cblProject.DataBind();
   }
}

1
投票

我认为问题是如何在ASP.net中完成,而不是从代码背后。

即:Gridviewdatasource,绑定,但在gridview内有一个列有checkboxlist和自己的数据源。使用Eval("yourcolumn")尝试绑定到GridView的主数据源而不是checkboxlist

对不起,我没有答案,因为我也想确定一下。


0
投票

这是我的答案:

GridView代码是:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="id" DataSourceID="SqlDataSource1" 
    OnRowDataBound="GridView1_RowDataBound">

    <Columns>
        <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id" />
        <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
        <asp:TemplateField HeaderText="Subjects">
            <ItemTemplate>
                <asp:CheckBoxList ID="CheckBoxList1" runat="server"  DataTextField="fprefid" DataValueField="fprefid">
                </asp:CheckBoxList>

            </ItemTemplate>
        </asp:TemplateField>

    </Columns>
</asp:GridView>

这是C#代码:



    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBoxList cbl = (CheckBoxList)e.Row.FindControl("CheckBoxList1");
            object objTemp = GridView1.DataKeys[e.Row.RowIndex].Value as object;
            string id = objTemp.ToString();

            SqlConnection con1 = new SqlConnection(constr);
            SqlConnection con2 = new SqlConnection(constr);
            SqlCommand com1 = new SqlCommand("select prefid from preferences",con1);

            if (con1.State == ConnectionState.Closed)
            con1.Open();

            if (con2.State == ConnectionState.Closed)
                con2.Open();

            SqlDataReader dreader1 = com1.ExecuteReader();



            if(dreader1.HasRows)
            {
                while(dreader1.Read())
                {                   
                    SqlCommand com2 = new SqlCommand("select fprefid from person_pref where fpersonid='" + id + "' and fprefid = '" + dreader1["prefid"] + "'", con2);
                    SqlDataReader dreader2 = com2.ExecuteReader();

                    ListItem li = new ListItem();

                    if (dreader2.HasRows)
                    {
                        li.Text = dreader1["prefid"].ToString();
                        li.Selected = true;
                    }
                    else
                    {
                        li.Text = dreader1["prefid"].ToString();
                        li.Selected = false;
                    }

                    cbl.Items.Add(li);
                    dreader2.Close();
                }
            }
        }        

    }
© www.soinside.com 2019 - 2024. All rights reserved.