我有一个网格视图,我想在其中的文本框中写一些备注
Gridview
。所以我写了下面的代码。
protected void btnRejectSite_Click(object sender, EventArgs e)
{
string strRemarks = string.Empty; string strID = string.Empty; string strCurrentUser = string.Empty;
foreach (GridViewRow row in rptHotoIPDataInfo.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
strID = (row.Cells[1].FindControl("lbl_ID") as Label).Text;
TextBox txtRemarks = (TextBox)row.FindControl("txtRemarks");
strRemarks = txtRemarks.Text; // here i want the text
strCurrentUser = CurrentUserName;
int intGroupID = CurrentUserGroupID;
}
}
}
但是当我调试
txtRemarks.Text
时,我总是获得 null
那样的价值。
下面是Gridview aspx代码。
<asp:GridView ID="rptHotoIPDataInfo" runat="server" AutoGenerateColumns="false" CssClass="appRejTableOuter appRejTable" Visible="true"
AllowPaging="true" PageSize="20" OnPageIndexChanging="rptHotoIPDataInfo_PageIndexChanging"
DataKeyNames="SAP_ID" OnRowEditing="rptHotoIPDataInfo_RowEditing" OnRowUpdating="rptHotoIPDataInfo_RowUpdating"
OnRowCancelingEdit="rptHotoIPDataInfo_RowCancelingEdit" OnRowDataBound="rptHotoIPDataInfo_RowDataBound"
OnRowCommand="rptHotoIPDataInfo_RowCommand" EnableViewState="true">
<Columns>
<asp:TemplateField HeaderStyle-CssClass="appRejTable">
<ItemTemplate>
<asp:CheckBox ID="chkRow" runat="server" Checked="false" AutoPostBack="true" CssClass="appRejCheckbox" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" ItemStyle-Width="50px" Visible="false">
<ItemTemplate>
<asp:Label ID="lbl_ID" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rejected Remarks" HeaderStyle-CssClass="appRejTable">
<ItemTemplate>
<asp:TextBox ID="txtRemarks" Text='<%# Eval("REJECT_REMARKS") %>' runat="server" CssClass="appRejInput"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="SAP_ID" HeaderText="SAP ID"></asp:BoundField>
<asp:BoundField DataField="CHANGEREQUEST_ID" HeaderText="Change Request Number"></asp:BoundField>
<asp:BoundField DataField="R4GSTATE_CODE" HeaderText="State Code"></asp:BoundField>
<asp:BoundField DataField="HOTO_STATUS" HeaderText="Hoto Status"></asp:BoundField>
<asp:BoundField DataField="CR_Justifications" HeaderText="CR Justifications"></asp:BoundField>
<asp:BoundField DataField="APPROVE_REJECT" HeaderText="Status"></asp:BoundField>
<asp:BoundField DataField="REJECTED_BY" HeaderText="Last Updated by"></asp:BoundField>
</Columns>
</asp:GridView>
请提出这里有什么问题
这是代码的修订版本,它使用层次结构在
TextBox
中查找 GridView
:
protected void btnRejectSite_Click(object sender, EventArgs e)
{
string strRemarks = string.Empty;
string strID = string.Empty;
string strCurrentUser = string.Empty;
foreach (GridViewRow row in rptHotoIPDataInfo.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
strID = (row.Cells[1].FindControl("lbl_ID") as Label).Text;
TextBox txtRemarks = (TextBox)row.FindControl("txtRemarks");
if (txtRemarks != null)
{
strRemarks = txtRemarks.Text;
// Your other logic here
}
strCurrentUser = CurrentUserName;
int intGroupID = CurrentUserGroupID;
}
}
}
确保
btnRejectSite_Click
方法正确连接到标记或代码隐藏中按钮的单击事件。如果问题仍然存在,您可能需要检查是否有任何可能干扰正常回发行为的客户端 JavaScript。