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中检索相关数据的功能.
|
请发表评论