You must Sign In to post a response.
  • Category: ASP.NET

    In this code how can i get the Text of Button in C#

    hi developers i am using this code it is working fine for me , but i am having a small doubt .

    protected void Page_Load(object sender, EventArgs e)
    {
    string query = string.Format("SELECT roomno from AD_Rooms_Master order by roomno");
    try
    {
    DataTable dt = GetData(query);
    for (int i = 0; i < dt.Rows.Count; i++)
    {
    Button btn = new Button();
    btn.ID = "btn" + (i + 1);
    btn.Text = dt.Rows[i]["roomno"].ToString();
    btn.Click += btnLoadButton_Click;
    pnl_btns.Controls.Add(btn);
    }
    }
    catch (Exception ex)
    {
    ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
    }
    finally
    {
    //cmd = null;
    //conn.Close();
    }
    }

    protected void btnLoadButton_Click(object sender, EventArgs e)
    {
    ClientScript.RegisterClientScriptBlock(this.GetType(), "button", "<script type = 'text/javascript'>alert('Room no " + (sender as Button).Text + " selected');</script>");
    }

    could you tell me instead of this javascript how can i get the value by using c# code , because then only i am validate the answer is correct or wrong .

    ClientScript.RegisterClientScriptBlock(this.GetType(), "button", "<script type = 'text/javascript'>alert('Room no " + (sender as Button).Text + " selected');</script>");

    thanks with
    Paul.S
    'Man becomes what he thinks about'
  • #769483
    sorry friends i have done with this use the following query

    Button btn = (Button)sender;
    string name = btn.Text;

    thanks to all

  • #769485
    you can do it like this
    protected void btnLoadButton_Click(object sender, EventArgs e)
    {
    Button btnload = (Button)sender;
    string btnloadtext = btnload.Text;
    string btnloadid = btnload.ID;
    }

    Hope this will help you.
    Thank You,


  • Sign In to post your comments