1,設定GridView1中做為參數值傳遞的列為模板列:
<asp:TemplateField HeaderText="地區" SortExpression="Territory">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
OnCommand="GridView1_onClick"
CommandArgument='<%# (String)DataBinder.Eval(Container,"DataItem.Territory") %>'
CommandName="TerritoryDrill"
Text='<%# (String)DataBinder.Eval(Container,"DataItem.Territory") %>'></asp:LinkButton>
</ItemTemplate>
<ItemStyle Height="15px" HorizontalAlign="Left" Width="150px" />
</asp:TemplateField>
2,獲得GridView1中點擊的行索引號:
protected void GridView1_onClick(object sender, CommandEventArgs e)
{
string TerritoryName = e.CommandArgument.ToString().Trim();
LinkButton lb = (LinkButton)sender;
DataControlFieldCell dcfc = (DataControlFieldCell)lb.Parent;
GridViewRow gvr = (GridViewRow)dcfc.Parent;
GridView1.SelectedIndex = gvr.RowIndex;
}
3,設定GridView2的資料來源的參數值:
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="Territory"
PropertyName="SelectedValue" DefaultValue="" Type="String" />
</SelectParameters>
這樣當使用者點擊GridView1中的某行中的LinkButton控制項,就會觸發GridView1_onClick事件,該事件可以獲得點擊行的索引號,當GridView1.SelectedIndex值發生變化時,就會觸發GridView2控制項的DataBound事件,並將點擊行的索引號傳遞給GridView2的資料來源的參數.這樣就實現了點擊GridView1,在GridView2中檢索相關資料的功能.
利用Session變數實現兩個控制項之間的傳值.
設定FormView1的資料來源的參數值:
<SelectParameters>
<asp:SessionParameter Name="EmployeeID" SessionField="EmployeeID" Type="Int32" />
</SelectParameters>
protected void GridView2_onClick(object sender, CommandEventArgs e)
{
string[] TerritoryName = e.CommandArgument.ToString().Trim();
Session["EmployeeID"] = TerritoryName;
FormView1.DataBind();
}
當點擊GridView2中的行中的傳值列時,取出行中的LinkButton控制項的參數並將它儲存到Session["EmployeeID"]中,並觸發FormView1的DataBound事件.從而實現點擊GridView2,在FormView1中檢索相關資料的功能.