skip to Main Content

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


  1. Here’s revised version of your code that uses the hierarchy to find the TextBox within the 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;
            }
        }
    }
    

    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.

    if (string.IsNullOrEmpty(strRemarks))
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Remarks cannot be empty.');", true);
        return; // or handle accordingly
    }
    
    Login or Signup to reply.
  2. 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:

            CheckBox chkRow = (CheckBox)row.FindControl("chkRow");
            Label lblID = (Label)row.FindControl("lbl_ID");
            TextBox txtRemarks = (TextBox)row.FindControl("txtRemarks");
    

    So, now you can use chkRow.Checked, and use the .text property of the label and textbox.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search