簡體   English   中英

來自GridView文本框的ASP.NET值是否在另一個類中使用?

[英]ASP.NET Value from GridView textbox used in another class?

我正在嘗試從GridView1訪問文本框值,以便可以在另一個名為“ BILL”的窗口中使用它

public void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
  if (e.CommandName.Equals("Pay"))

            {

                    GridViewRow row = (sender as Control).NamingContainer as GridViewRow;
                    //cannot use RowIndex

                    string fname = ((TextBox)row.FindControl("txtForename")).Text.Trim();
                    Response.Redirect("~/Bill.aspx?lblF=" + fname);


            }
}

這段代碼來自我要從文本框中獲取值並將其設置為字符串的主頁。 單擊“支付”按鈕時,該值應被轉移到“賬單”頁面中,成為標簽lblF的值。

當我運行程序並單擊“支付”按鈕時,它會輸出錯誤“對象引用未設置為對象實例”。 這意味着字符串fname為空。 但是我很困惑為什么?

html代碼:

        OnrowCommand="GridView1_RowCommand" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting"    BackColor="White" BorderColor="#006699" BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal"
        >

    <Columns>
        <asp:TemplateField HeaderText="Forename">
            <ItemTemplate>
                <asp:Label Text='<%# Eval("Forename") %>' runat="server" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtForename" Text='<%# Bind("Forename") %>' runat="server"/>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtForeNameFooter" runat="server" />
            </FooterTemplate>                
        </asp:TemplateField>

    <Columns>

因為發送方是GridView,所以獲取命名容器的代碼行返回null。 這就是為什么您會得到一個錯誤。 使用RowCommand事件中的e.Row已經可以使用該行。

另外,當您單擊“付款”按鈕時,該行可能未處於編輯模式。 僅當該行處於編輯模式時,您要查找的文本框才存在。 當row不處於編輯模式時,則正在渲染ItemTemplate控件,該控件是您標記中的標簽控件。

您應該檢查是否行。 FindControl("txtForename")為null,如果找到,則為同一列查找標簽控件。 但是,您應該在ItemTemplate中為標簽控件提供一個ID,該ID現在在您的標記中丟失。

您的代碼還有其他問題。

  • 當您的C#代碼執行時,您處於RowCommand事件中,因此您只需使用e.Row就可以訪問該行,而無需NamingContainer邏輯。
  • 另外,如果con是數據庫連接對象,那么using(con)不會有任何作用,因為您沒有連接到任何數據庫,而只是重定向到另一個頁面。

使用具有ID的標簽控件進行標記

<asp:TemplateField HeaderText="Forename">
            <ItemTemplate>
                <asp:Label ID="lblForename" Text='<%# Eval("Forename") %>' runat="server" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtForename" Text='<%# Bind("Forename") %>' runat="server"/>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtForeNameFooter" runat="server" />
            </FooterTemplate>                
</asp:TemplateField>

C#代碼獲取姓氏文本

if (e.CommandName.Equals("Pay")) {
  //you don't need NamingContainer to get Row in RowCommand event
 //since its already available under the GridViewCommandEventArgs object

  //you can get the GridViewRow using the line below
   GridViewRow row = (e.CommandSource as Control).NamingContainer as GridViewRow;

  if (row != null) {
  string fname = null;

  //check if row is in edit mode by checking if textbox exists
  if (row.FindControl("txtForename") != null) {
   fname = ((TextBox) row.FindControl("txtForename")).Text.Trim();
  } else {
   fname = ((Label) row.FindControl("lblForename")).Text.Trim();
  }

   Response.Redirect("~/Bill.aspx?lblF=" + fname);
  }
 }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM