简体   繁体   English

显示信息 在Gridview中asp.net

[英]Displaying Info. In Gridview asp.net

I'm doing a web application in asp.net. 我在asp.net中做一个Web应用程序。 It is a fitness app. 这是一个健身应用程序。 where members register onto the site, insert different exercises they have been doing and they can also join a group of a fitness club. 会员在该网站上注册后,插入他们一直在做的不同练习,也可以加入健身俱乐部。

When I register for a club, I have all of the members of that club displayed in a Gridview and I have the select button in the Gridview selected. 注册俱乐部时,该俱乐部的所有成员都显示在Gridview中,并且在Gridview中选择了选择按钮。 When the select button is hit, I want to be able to see what exercises people have been doing which is stored in the AssignPlan table ie Run, Gym work etc. 当按下选择按钮时,我希望能够看到人们进行的锻炼,这些锻炼存储在AssignPlan表中,即跑步,健身房工作等。

However when I display this information, I do it using a session to get the specific information of who ever is logged in. But for the groups I want people to see other people's information but sessions will not work for me as the person who's info. 但是,当我显示此信息时,我使用会话来获取有关登录者的特定信息。但是对于组,我希望人们看到其他人的信息,但是会话对我来说是无效的。 I want to bring up is not logged in. 我要调出未登录。

When I am displaying my own information, this is the code I use: 当我显示自己的信息时,这是我使用的代码:

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();

}

Is there any way of doing this? 有什么办法吗? Any help would be really appreciated! 任何帮助将非常感激!

You've 2 solutions: 您有2个解决方案:

  1. In the First Grid set the DataKeyNames property as User Id, then add another Grid and in its Data Source make a Where clause that use GridView1.SelectedValue to get user exercises. 在“第一个网格”中,将DataKeyNames属性设置为“用户ID”,然后添加另一个网格,并在其“数据源”中创建一个使用GridView1.SelectedValue的Where子句来进行用户练习。 Here's an example when you select company the 2nd GridView will display its contact persons 这是一个示例,当您选择公司时,第二个GridView将显示其联系人

  2. Add hyperlink row to your GridView which Url for another form where you'll display user's exercises and pass user Id as QueryString. 将超链接行添加到GridView的另一种形式的Url,您将在其中显示用户的练习并将用户ID作为QueryString传递。

Example on solution 1: 解决方案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>

Example on solution 2: 解决方案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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM