显示信息。在Gridview asp.net中

时间:2014-02-06 20:27:27

标签: c# asp.net gridview

我在asp.net上做一个web应用程序。这是一个健身应用程序。会员注册到网站,插入他们一直在做的不同的练习,他们也可以加入一个健身俱乐部组。

当我注册俱乐部时,我将该俱乐部的所有成员显示在Gridview中,并且我选择了Gridview中的选择按钮。点击选择按钮后,我希望能够看到人们一直在做什么,这些练习存储在AssignPlan表中,即Run,Gym work等。

然而,当我显示这些信息时,我会使用会话来获取有关谁登录的具体信息。但是对于组,我希望人们看到其他人的信息,但会话对我来说不适合作为人谁的信息。我想提起来还没有登录。

当我显示自己的信息时,这是我使用的代码:

SqlDataAdapter dadapter;
DataSet dset;

SqlConnection con = new SqlConnection(@"ConnectionString");
string sql = "select * from ExerciseType";

protected void Page_Load(object sender, EventArgs e)
{
    lblRegistered.Text = Session["Name"].ToString();

    if (!IsPostBack)
    {
        dadapter = new SqlDataAdapter(sql, con);
        dset = new DataSet();
        dadapter.Fill(dset);
        DropDownList1.DataSource = dset.Tables[0];
        DropDownList1.DataTextField = "ExerciseType";
        DropDownList1.DataValueField = "ExerciseType";
        DropDownList1.DataBind();

        GridViewBind();

    }
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewBind();
}


public void GridViewBind()
{

    dadapter = new SqlDataAdapter("SELECT ExerciseName FROM ExerciseDisplay Where TypeName = '" + DropDownList1.SelectedValue + "'", con);

    dset = new DataSet();
    dadapter.Fill(dset);
    GridView1.DataSource = dset.Tables[0];
    GridView1.DataBind();

}

有没有办法做到这一点?任何帮助都会非常感激!

1 个答案:

答案 0 :(得分:0)

你有2个解决方案:

  1. 在First Grid中将DataKeyNames属性设置为User Id,然后添加另一个Grid,并在其Data Source中创建一个使用GridView1.SelectedValue的Where子句来获取用户练习。 这是一个例子,当您选择公司时,第二个GridView将显示其联系人

  2. 在您的GridView中添加超链接行,在其中您将显示用户练习的另一个表单,并将用户ID作为QueryString传递。

  3. 解决方案1的示例:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Comp_CompanyId" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="Comp_CompanyId" HeaderText="Comp_CompanyId" ReadOnly="True" SortExpression="Comp_CompanyId"></asp:BoundField>
            <asp:BoundField DataField="Comp_Name" HeaderText="Comp_Name" SortExpression="Comp_Name"></asp:BoundField>
            <asp:BoundField DataField="Comp_Type" HeaderText="Comp_Type" SortExpression="Comp_Type"></asp:BoundField>
            <asp:BoundField DataField="Comp_Status" HeaderText="Comp_Status" SortExpression="Comp_Status"></asp:BoundField>
            <asp:ButtonField CommandName="Select" Text="Select" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:CRMConnectionString %>' SelectCommand="SELECT [Comp_CompanyId], [Comp_Name], [Comp_Type], [Comp_Status] FROM [Company]"></asp:SqlDataSource>
    
    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2">
        <Columns>
            <asp:BoundField DataField="Pers_FirstName" HeaderText="Pers_FirstName" SortExpression="Pers_FirstName" />
            <asp:BoundField DataField="Pers_LastName" HeaderText="Pers_LastName" SortExpression="Pers_LastName" />
            <asp:BoundField DataField="Pers_Salutation" HeaderText="Pers_Salutation" SortExpression="Pers_Salutation" />
            <asp:BoundField DataField="Pers_Title" HeaderText="Pers_Title" SortExpression="Pers_Title" />
            <asp:BoundField DataField="Pers_MiddleName" HeaderText="Pers_MiddleName" SortExpression="Pers_MiddleName" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:CRMConnectionString %>" SelectCommand="SELECT [Pers_FirstName], [Pers_LastName], [Pers_Salutation], [Pers_Title], [Pers_MiddleName] FROM [Person] WHERE ([Pers_CompanyId] = @Pers_CompanyId)">
        <SelectParameters>
            <asp:ControlParameter ControlID="GridView1" Name="Pers_CompanyId" PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
    

    解决方案2的示例:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Comp_CompanyId" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:TemplateField HeaderText="Comp_CompanyId" SortExpression="Comp_CompanyId">
                <EditItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("Comp_CompanyId") %>'></asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:HyperLink ID="lnkCompany_Id" runat="server" Text='<%# Bind("Comp_CompanyId") %>' 
                        NavigateUrl='<%# Eval("Comp_CompanyId","Persons.aspx?CompanyId={0}") %>'></asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Comp_Name" HeaderText="Comp_Name" SortExpression="Comp_Name"></asp:BoundField>
            <asp:BoundField DataField="Comp_Type" HeaderText="Comp_Type" SortExpression="Comp_Type"></asp:BoundField>
            <asp:BoundField DataField="Comp_Status" HeaderText="Comp_Status" SortExpression="Comp_Status"></asp:BoundField>
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:CRMConnectionString %>' SelectCommand="SELECT [Comp_CompanyId], [Comp_Name], [Comp_Type], [Comp_Status] FROM [Company]"></asp:SqlDataSource>