Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i enable textbox nested inside Gridview in RowEditing event ?

C#
protected void MyGrid_RowEditing(object sender, GridViewEditEventArgs e)
        {
            string CampID;
            //int id = Convert.ToInt32(MyGrid.DataKeys[e.].ToString());
            CampID = (Session["CampaignID"].ToString());
            mf.UpdateStatus(CampID, 'X');
            MyGrid.EditIndex = e.NewEditIndex;
            //string lblOfferType = MyGrid.Rows[e.NewEditIndex].Cells[4].Text;
            GridViewRow currentRow = this.MyGrid.Rows[e.NewEditIndex];

            string lblOfferType = (currentRow.FindControl("lblOffer_Type") as Label).Text;
            //string freeQty = (currentRow.FindControl("txtFree_Qty") as TextBox).Text;
            //string discPercent = (currentRow.FindControl("txtDisc_Percent") as TextBox).Text;
            //TextBox discValue = (currentRow.FindControl("txtDisc_Value") as TextBox);
            //string offerPrice = (currentRow.FindControl("txtOffer_Price") as TextBox).Text;
            //string freeArticle = (currentRow.FindControl("txtFree_Article") as TextBox).Text;

            if (lblOfferType == "VALUE DISCOUNT")
            {
                GridViewRow row = (GridViewRow)((Control)sender).NamingContainer;
                GridViewRow currentRow1 = this.MyGrid.Rows[e.NewEditIndex];
                ((TextBox)MyGrid.Rows[e.NewEditIndex].FindControl("txtDisc_Value")).Enabled = false;
                //discValue.Enabled = true;
                //(currentRow.FindControl("txtDisc_Value") as TextBox).Enabled = true;
                //((TextBox)currentRow.FindControl("txtDisc_Value")).Enabled = true;
            }
}



Error: Object reference not set to an instance.

Please provide the solution.

What I have tried:

C#
protected void MyGrid_RowEditing(object sender, GridViewEditEventArgs e)
        {
            string CampID;
            //int id = Convert.ToInt32(MyGrid.DataKeys[e.].ToString());
            CampID = (Session["CampaignID"].ToString());
            mf.UpdateStatus(CampID, 'X');
            MyGrid.EditIndex = e.NewEditIndex;
            //string lblOfferType = MyGrid.Rows[e.NewEditIndex].Cells[4].Text;
            GridViewRow currentRow = this.MyGrid.Rows[e.NewEditIndex];

            string lblOfferType = (currentRow.FindControl("lblOffer_Type") as Label).Text;
            //string freeQty = (currentRow.FindControl("txtFree_Qty") as TextBox).Text;
            //string discPercent = (currentRow.FindControl("txtDisc_Percent") as TextBox).Text;
            //TextBox discValue = (currentRow.FindControl("txtDisc_Value") as TextBox);
            //string offerPrice = (currentRow.FindControl("txtOffer_Price") as TextBox).Text;
            //string freeArticle = (currentRow.FindControl("txtFree_Article") as TextBox).Text;

            if (lblOfferType == "VALUE DISCOUNT")
            {
                GridViewRow row = (GridViewRow)((Control)sender).NamingContainer;
                GridViewRow currentRow1 = this.MyGrid.Rows[e.NewEditIndex];
                ((TextBox)MyGrid.Rows[e.NewEditIndex].FindControl("txtDisc_Value")).Enabled = false;
                //discValue.Enabled = true;
                //(currentRow.FindControl("txtDisc_Value") as TextBox).Enabled = true;
                //((TextBox)currentRow.FindControl("txtDisc_Value")).Enabled = true;
            }
}
Posted
Updated 22-Mar-18 20:38pm
v2

try this
TextBox txtDisc_Value =(TextBox)MyGrid.Rows[e.NewEditIndex].FindControl("txtDisc_Value");
txtDisc_Value.Enabled = false;
 
Share this answer
 
v2
Thanks, but this error is showing after executing your line of code:-

"Object reference not set to an instance of an object."
 
Share this answer
 
Comments
dkamble 10358990 22-Mar-18 5:02am    
use RowDataBound Event

https://stackoverflow.com/questions/20892053/how-to-enable-or-disable-text-box-in-gridview-using-c-sharp
Member 13439076 22-Mar-18 5:10am    
Thank you!!! It work for me
Richard Deeming 23-Mar-18 13:10pm    
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution. DO NOT post your reply as a new "solution".

Delete this non-solution.
Please provide better solution to the problem.
Thanks in Advance.
 
Share this answer
 
Comments
Richard Deeming 23-Mar-18 13:10pm    
And this one.
<asp:TemplateField HeaderText="Discount Value">
<ItemTemplate>
<asp:Label ID="lblDisc_Value" runat="server" Text='<%# Bind("Disc_Value") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDisc_Value" runat="server" Text='<%# Bind("Disc_Value") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
 
I am able to find the value of lblDisc_Value but unable to Enable txtDisc_Value to false or true
 
Share this answer
 
Comments
Richard Deeming 23-Mar-18 13:10pm    
And this one.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900