I have a gridview where I want to write some remarks in textbox which is inside that 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;
}
}
}
But when I debug the txtRemarks.Text
I am getting null
always.
This is the gridview aspx markup:
<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>
What is wrong here?
2
Answers
Here’s revised version of your code that uses the hierarchy to find the
TextBox
within theGridView
:Ensure that the
btnRejectSite_Click
method is properly wired up to the button’s click event in your markup or code-behind. If the issue persists, you might want to check for any client-side JavaScript that might interfere with the normal postback behavior.For showing an alert if remarks is null, you can use JavaScript to show a client-side alert. You can use
ScriptManager
to register a startup script.Ok, so the rule here is:
bound/built in columns, you use cells() collection.
Template fields, you do NOT use cells() collection, and you use Row.FindControl("control name").
Hence this should work:
So, now you can use chkRow.Checked, and use the .text property of the label and textbox.