amitesh
amitesh

Reputation: 860

how can i change the text on button at run time after click on hyperlink

i have two text-boxes a button and a gridview. Q.1 When user enter details in the text-boxes and press submit button i want to update grid-view accordingly Q.2 When user hits "Edit" link which is present in the gridview, i would like to change the text of submit button to Update button. how can i do that thanks in advance

what i have tried yet:

aspx.cs code:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        this.BindGrid();
        string ID = Request.QueryString["ID"];
        cmd = new SqlCommand("Select * from UserDetails where ID='" + ID + "'", con);
        con.Open();
        ad = new SqlDataAdapter(cmd);
        dt.Clear();
        ad.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            tbid.Text = ID;
            TextBox1.Text = dt.Rows[0][1].ToString();
            TextBox2.Text = dt.Rows[0][2].ToString();
        }
        con.Close();
    }
}  
protected void btnSubmit_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True");
    con.Open();

    string Name = TextBox1.Text;
    string Place = TextBox2.Text;

    using (SqlCommand cmd = con.CreateCommand())
    {
        cmd.CommandText = "insert into UserDetails(Name,Place) values('" + Name + "','" + Place + "')";
        cmd.Parameters.AddWithValue("@Name", TextBox1.Text);
        cmd.Parameters.AddWithValue("@Place", TextBox2.Text);
        cmd.ExecuteNonQuery();
        Label1.Text = "Record Successfully inserted";
    }
    con.Close();
    btnSubmit.Text = "Update";
    TextBox1.Text = string.Empty;
    TextBox2.Text = string.Empty;
    }
private void BindGrid()
{
    con.Open();
    ad = new SqlDataAdapter("Select * from UserDetails", con);
    ad.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    con.Close();
}

Upvotes: 1

Views: 422

Answers (2)

Paritosh
Paritosh

Reputation: 4503

After the user submits new data you can try to call your bindgrid method again, this way it will rebind after the new data is saved. For the edit piece, GridView has an edit template, you can try using that: http://msdn.microsoft.com/en-us/library/ms972948.aspx

Upvotes: 0

Pedro Dusso
Pedro Dusso

Reputation: 2140

Call a Refresh() doesn't help? I'm not sure about ASP.NET, but you have to do it in Forms.

Upvotes: 1

Related Questions