2012-11-06 27 views
0

我有這樣一個gridview:使用C#將數據從gridview傳輸到asp.net中的另一個webform?

<asp:GridView ID="wbsdataGV1" runat="server" Width="790px" AutoGenerateColumns="False" 
            OnSelectedIndexChanged="wbsdataGV1_SelectionChanged"> 
            <Columns> 

             <asp:TemplateField HeaderText="WBS Code"> 
              <ItemTemplate> 
               <asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Eval("WBSCode") %>'></asp:LinkButton> 
              </ItemTemplate> 
             </asp:TemplateField> 
             <asp:TemplateField HeaderText="Description"> 
              <ItemTemplate> 
               <asp:TextBox ID="TextBox7" runat="server" Text='<%# Eval("Description") %>'></asp:TextBox> 
              </ItemTemplate> 
             </asp:TemplateField> 
             <asp:TemplateField HeaderText="Territory Code"> 
              <ItemTemplate> 
               <asp:TextBox ID="TextBox8" runat="server" Text='<%# Eval("TerritoryCode") %>'></asp:TextBox> 
              </ItemTemplate> 
             </asp:TemplateField> 
             <asp:TemplateField HeaderText="Amount"> 
              <ItemTemplate> 
               <asp:TextBox ID="TextBox9" runat="server" Text='<%# Eval("AmountReleased") %>'></asp:TextBox> 
              </ItemTemplate> 
             </asp:TemplateField> 
            </Columns> 
           </asp:GridView> 

當我點擊LinkBut​​ton的,我想這些數據,我會通過

protected void wbsdataGV1_SelectionChanged(object sender, EventArgs e) 
{ 
    //Some code ; 
} 

現在我的問題是轉移到另一個ASPX從..我如何通過點擊gridview上的linkbutton來獲取這些數據?我可以通過查詢字符串或會話變量或隱藏文本字段傳輸數據......但我擔心的是,如何才能得到確切的點擊數據... 有什麼幫助?

回答

1

定義DataKeys屬性數據的是主鍵(在這裏我假設你有一個名爲「ID」行標識符。

<asp:GridView ... DataKeys="ID"> 
    ... existing markup 
</asp:GridView> 

wbsdataGV1_SelectionChanged,找回用戶點擊該行的ID像這樣:

int rowID = (int)this.wbsdataGV1.SelectedDataKey.Value; 

然後重定向至其他頁面,只是路過的ID:

Response.Redirect("anotherpage.aspx?id=" + rowID); 

然後在其他網頁:

protected void Page_Load(object s, EventArgs e) 
{ 
    if (!Page.IsPostback) 
    { 
     int rowID = int.Parse(Request.QueryString["id"]); 

     // do something with the ID of the row, like go and look 
     // up JUST that row again 
    } 
} 

獎金 - 使整個排點擊,避免了一個命令的需要,這樣做:

<script language="javascript" type="text/javascript"> 
    var oldColour = null; 

    function rowMouseover(o) { 
     o.style.cursor = 'pointer'; 
     oldColour = o.style.backgroundColor; 
     o.style.backgroundColor = '#dddddd'; 
    } 

    function rowMouseout(o) { 
     o.style.backgroundColor = oldColour; 
    } 
</script> 

<asp:GridView ... OnRowCreated="grid_RowCreated" /> 

protected void grid_RowCreated(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     e.Row.Attributes["onclick"] = 
      Page.ClientScript.GetPostBackClientHyperlink(
       this.grid, 
       "Select$" + e.Row.RowIndex); 

     e.Row.Attributes["onmouseover"] = "rowMouseover(this);"; 
     e.Row.Attributes["onmouseout"] = "rowMouseout(this);"; 
    } 
} 

protected override void Render(HtmlTextWriter writer) 
{ 
    for (int i = 0; i < grid.Rows.Count; i++) 
     Page.ClientScript.RegisterForEventValidation(grid.UniqueID, "Select$" + i); 

    base.Render(writer); 
} 

你可以通過Session對象通過ID ,但是在多個瀏覽器窗口同時執行相同操作時(取決於瀏覽器),您會得到奇怪的行爲。這對用戶來說很難篡改。

您也可以通過Request.Form傳遞ID,這會將其從繁瑣的用戶f hide中隱藏起來,但不會增加任何真正的安全性。 Protip:不只是將大量對象轉儲到Session變量中。它很懶惰,容​​易出錯,並且不能縮放。

+0

對於我評論過的那個人 - 你真的幼稚到足以讓我的回答在刪除自己的回答後降低了嗎? – tomfanning

+0

好,謝謝,我會嘗試這一..但這個做我需要添加'的在我的GridView bcoz當我這樣做 – Drone

+0

是的,沒錯'wbsdataGV1_SelectionChanged'只有射擊'。 – tomfanning

相關問題