I am trying to access a textbox value from GridView1 so that I can use it in another window called "BILL"
我试图从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);
}
}
This code is from the home page where I want to get the value from the textbox and set it as a string. When the button "pay" is clicked, the value should be transferred to the "BILL" page into label lblF's value.
此代码来自主页,我想从文本框中获取值并将其设置为字符串。单击“pay”按钮时,该值应转移到“BILL”页面,转换为标签lblF的值。
When I run the program and click on the "pay" button it outputs error 'object reference not set to an instance of an object'. This means that the string fname is empty. But I am confused why?
当我运行程序并单击“付款”按钮时,它输出错误'对象引用未设置为对象的实例'。这意味着字符串fname为空。但我很困惑为什么?
html code:
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>
1 个解决方案
#1
0
The line of code that gets the naming container is returning null since the sender is a GridView. And that is why you get an error. The row is already available using e.Row
in your RowCommand event.
获取命名容器的代码行返回null,因为发送方是GridView。这就是你得到错误的原因。在RowCommand事件中使用e.Row已经可以使用该行。
Also, when you click on Pay button, the row may not be in edit mode. The textbox you are seeking is only there when the row is in edit mode. When row is not in edit mode, then the ItemTemplate control is rendering which is a label control from your markup.
此外,当您单击“付款”按钮时,该行可能不处于编辑模式。您正在寻找的文本框仅在行处于编辑模式时才会出现。当行不处于编辑模式时,ItemTemplate控件将呈现,这是标记中的标签控件。
You should check if row. FindControl("txtForename")
is null, and if it is then find the label control for same column. But, you should give the label control an id in ItemTemplate, which is right now missing in your markup.
你应该检查一下行。 FindControl(“txtForename”)为null,如果是,则查找同一列的标签控件。但是,您应该在ItemTemplate中为标签控件提供一个id,它现在在您的标记中缺失。
There are other issues with your code.
您的代码还有其他问题。
- You are in
RowCommand
event when your C# code executes and so you already have access to the row by simply usinge.Row
and no need for NamingContainer logic. - 当您的C#代码执行时,您处于RowCommand事件中,因此您只需使用e.Row就可以访问该行,而不需要NamingContainer逻辑。
- Also,
using(con)
doesn't serve any purpose ifcon
is a database connection object since you are not connecting to any database but simply redirecting to another page. - 此外,如果con是数据库连接对象,则使用(con)不起任何作用,因为您没有连接到任何数据库而只是重定向到另一个页面。
Markup with label control having an id
标签控件标记为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# code to get the text for forename
C#代码获取forename的文本
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);
}
}
}
#1
0
The line of code that gets the naming container is returning null since the sender is a GridView. And that is why you get an error. The row is already available using e.Row
in your RowCommand event.
获取命名容器的代码行返回null,因为发送方是GridView。这就是你得到错误的原因。在RowCommand事件中使用e.Row已经可以使用该行。
Also, when you click on Pay button, the row may not be in edit mode. The textbox you are seeking is only there when the row is in edit mode. When row is not in edit mode, then the ItemTemplate control is rendering which is a label control from your markup.
此外,当您单击“付款”按钮时,该行可能不处于编辑模式。您正在寻找的文本框仅在行处于编辑模式时才会出现。当行不处于编辑模式时,ItemTemplate控件将呈现,这是标记中的标签控件。
You should check if row. FindControl("txtForename")
is null, and if it is then find the label control for same column. But, you should give the label control an id in ItemTemplate, which is right now missing in your markup.
你应该检查一下行。 FindControl(“txtForename”)为null,如果是,则查找同一列的标签控件。但是,您应该在ItemTemplate中为标签控件提供一个id,它现在在您的标记中缺失。
There are other issues with your code.
您的代码还有其他问题。
- You are in
RowCommand
event when your C# code executes and so you already have access to the row by simply usinge.Row
and no need for NamingContainer logic. - 当您的C#代码执行时,您处于RowCommand事件中,因此您只需使用e.Row就可以访问该行,而不需要NamingContainer逻辑。
- Also,
using(con)
doesn't serve any purpose ifcon
is a database connection object since you are not connecting to any database but simply redirecting to another page. - 此外,如果con是数据库连接对象,则使用(con)不起任何作用,因为您没有连接到任何数据库而只是重定向到另一个页面。
Markup with label control having an id
标签控件标记为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# code to get the text for forename
C#代码获取forename的文本
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);
}
}
}