krishb591993
krishb591993

Reputation: 303

How to get textbox value in gridview on clicking edit button?

Iam new to asp.net and I have been trying the above question for some time without any success. here's my code in the roweditevent

 GridView1.EditIndex = e.NewEditIndex;
 int newindex = e.NewEditIndex;
 TextBox NAME = GridView1.Rows[newindex].FindControl("txtboxname") as TextBox;
 string gridupdat = NAME.Text;

But on debugging I always get null reference there.

this is my row update code and it's working fine

Label ID = GridView1.Rows[e.RowIndex].FindControl("ID") as Label;
    TextBox NAME = GridView1.Rows[e.RowIndex].FindControl("txtboxname") as TextBox;
    DropDownList STATUS = GridView1.Rows[e.RowIndex].FindControl("dropdownstatus") as DropDownList;
    string string1 = NAME.Text;
    if (fetchmail(string1, labelgrid999) == true)
    {
        string updatquery = string.Format("UPDATE Compliance_Tracker.dbo.verificationMaster SET NAME='{0}',STATUS='{1}' WHERE ID = {2}", NAME.Text, STATUS.Text, Convert.ToInt32(ID.Text));
        string dupquery = "select COUNT(*) from Compliance_Tracker.dbo.verificationMaster where Compliance_Tracker.dbo.verificationMaster.NAME = '" + NAME.Text + "';";
        if (obj4.isDuplicate(dupquery) == false )
        {
            GridView1.EditIndex = -1;
            string populatequery = updatquery + ";select NAME,(case when STATUS='1' then 'Active' when STATUS='0'then 'Inactive' ELSE 'UNKNOWN' END)as STATUS,ID from Compliance_Tracker.dbo.verificationMaster;";
            obj4.BindGridData(populatequery, GridView1);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + "Username already exists" + "');", true);
        }
    }
    else
    {
        string myStringVariable = "Please enter valid username";
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);
    }

Upvotes: 2

Views: 5219

Answers (3)

Krunal Patil
Krunal Patil

Reputation: 3676

This is what i do when I try to fetch the data from textbox on Edit Button click:

<asp:TemplateField HeaderText="Edit">
   <ItemTemplate>
      <asp:Button ID="BtnEdit" CausesValidation="false" runat="server" Text="Update" CommandName="updateData" CommandArgument='<%# Eval("ID") %>' />
   </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Price">
   <ItemTemplate>
       <asp:TextBox ID="TxtPriceEdit" runat="server" Text='<%# Eval("Product_Price") %>' Width="120px"></asp:TextBox>
   </ItemTemplate>
</asp:TemplateField>

Code Behind :

protected void GrdDataEdit_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "updateData")
    {
        // This will give you the ID of the record you are passing in the CommandArgument='<%# Eval("ID") %>'  
        int i = Convert.ToInt32(e.CommandArgument); 

        GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
        TextBox price = (TextBox)row.FindControl("TxtPriceEdit");
    }
}

Upvotes: 1

StackTrace
StackTrace

Reputation: 9416

The GridViewEventArgs has the index of the row being edited. It doesn't look like you are using the index from the event args. Try this:

protected void edit(object sender, GridViewEditEventArgs e)         
{

    string gridupdat = GridView1.Rows[e.NewEditIndex].Cells[0].Text;
    // where Cells[0] = the index of your "txtboxname" column
}

Upvotes: 0

deostroll
deostroll

Reputation: 11975

On row databound event track the row state and make the necessary calls to acquire the reference of the required control calling FindControl() on the GridViewRow object.

protected void gvMaint_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowState == DataControlRowState.Edit)
    {
        TextBox txtFreqMiles = (TextBox)e.Row.FindControl("txtFreqMiles");

        // At this point, you can change the value as normal
        txtFreqMiles.Text = "some new text";
    }
}

Code taken from here

Upvotes: 0

Related Questions