ASP.Net常見技術疑難解析(二)

來源:互聯網
上載者:User
  • 在兩個GridView控制項之間傳值(方法一)

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中檢索相關資料的功能.

  • 在兩個GridView控制項之間傳值(方法二)

利用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中檢索相關資料的功能.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.