2014-02-06 80 views
0

我在做asp.net中的web應用程序。這是一個健身應用程序。成員在網站上註冊,插入不同的練習,他們也可以加入一組健身俱樂部。顯示信息。在GridView中asp.net

當我註冊一個俱樂部時,我將該俱樂部的所有成員顯示在一個Gridview中,並且我在Gridview中選擇了選擇按鈕。當選擇按鈕被擊中時,我希望能夠看到人們在AssignPlan表中存儲的練習,即運行,健身房工作等。

但是,當我顯示此信息時,我使用會話來獲取誰登錄的具體信息。但是對於這些組,我希望人們能夠看到其他人的信息,但會話對於我來說不適合作爲信息的人。我想提出未登錄

當我展示我自己的信息,這是我使用的代碼:

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

} 

是否有這樣做的方法嗎?任何幫助將非常感激!

+0

不知道我完全明白你想要做的事情,但我認爲一個查詢字符串可以幫助你。您只需將該人員的ID作爲查詢字符串導航到新頁面。使用該ID來檢索該人的信息。 – codingbiz

+0

所以,爲了澄清,作爲一個小組成員的所有成員都有自己的練習,他們已經在練習表格中輸入了。我試圖點擊一個成員,並帶我到顯示他們已經插入的練習的練習形式。所以是的,我認爲你是在正確的軌道上檢索一個ID,也許把他們的用戶名放在一個標籤上,並從中檢索他們的練習。我將如何去檢索ID?我對C#非常陌生,並且在努力掙扎。你有任何可以幫助我的代碼嗎? – user3249809

+0

就像你從會話中獲得用戶名,從Gridview中的其他用戶列表中,你的鏈接可以有一個querystring部分,如'relateduser.aspx?username = john'。這會將您帶到另一個頁面,您可以從查詢字符串中檢索用戶名,並使用它來顯示用戶的練習。兩個頁面,一個使用來自會話的用戶名,另一個使用來自querystring的用戶名,但頁面具有相同的邏輯,只是用戶名的來源不同。 Google HyperlinkColumn for GridView關於如何從Gridview中的字段生成鏈接 – codingbiz

回答

0

您已經2個解決方案:

  1. 在第一個網格設置DataKeyNames屬性的用戶ID,然後添加另外的表格,並在其數據源使使用GridView1.SelectedValue獲取用戶的練習Where子句。 這裏有一個例子,當您選擇公司第二屆GridView控件將顯示其聯繫人

  2. 添加超級鏈接行你的GridView哪個URL爲另一種形式在這裏您可以顯示用戶的運動,並通過用戶ID作爲查詢字符串。在解決方案1 ​​

示例:解決方案2

<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> 

例子:

<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>