標籤:
一,當前頁面中,前台介面的主要代碼:
<asp:TemplateField HeaderText="姓名"> <ItemTemplate> <!--根據id去尋找詳細資料--> <asp:LinkButton id="hlinkName" runat="server" CommandName="getDetail" CommandArgument= ‘<%# Bind("id") %>‘ Text=‘<%# Bind("name") %>‘></asp:LinkButton> </ItemTemplate> </asp:TemplateField>
該代碼是GridView中的一部分,主要實現的是當使用者在‘姓名‘這一列對應的值上點擊時,跳轉到‘Detail.aspx‘頁面,並選擇當前行的詳細資料,顯示在‘Detail.aspx‘頁面中.
當前頁面GridView效果為:
實現GridView的後台代碼為:
/// <summary> /// 進入詳細頁面. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void gvw_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName.Equals("getDetail")) { //根據將當前行的id傳遞到另一個頁面中. Response.Redirect(@"~\Detail.aspx?id=" + e.CommandArgument); } }
二,Detail.aspx(目標)頁面中,
1,首先,在前台的GridView中,去掉GridView綁定資料來源的控制項AccessDataSource中的SelectCommand,並在後台代碼手動設定SelectCommand內容,如,可以在Pape_Load的時候,加入:
string id = Request.QueryString["id"]; //access 資料來源控制項的SelectCommand的後台綁定. AccessDataSrc.SelectCommand = "select * from [t_man] where [id][email protected]"; AccessDataSrc.SelectParameters.Clear(); //清除可能已有選擇參數. AccessDataSrc.SelectParameters.Add("id", TypeCode.Int32, id); AccessDataSrc.DataBind();
即可.
asp.net - GridView根據linkButton值不同跳轉不同頁面