Skip to content

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

Paul Rajs

Paul Rajs

Asked Jan 30, 2018, 11:14 PM EST1 answer
Share
LinkedInX
hi developers i am using this code it is working fine for me , but i am having a small doubt .
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. string query = string.Format("SELECT roomno from AD_Rooms_Master order by roomno");  
  4. try  
  5. {  
  6. DataTable dt = GetData(query);  
  7. for (int i = 0; i < dt.Rows.Count; i++)  
  8. {  
  9. Button btn = new Button();  
  10. btn.ID = "btn" + (i + 1);  
  11. btn.Text = dt.Rows[i]["roomno"].ToString();  
  12. btn.Click += btnLoadButton_Click;  
  13. pnl_btns.Controls.Add(btn);  
  14. }  
  15. }  
  16. catch (Exception ex)  
  17. {  
  18. ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);  
  19. }  
  20. finally  
  21. {  
  22. //cmd = null;  
  23. //conn.Close();  
  24. }  
  25. }  
  26.   
  27. protected void btnLoadButton_Click(object sender, EventArgs e)  
  28. {  
  29. ClientScript.RegisterClientScriptBlock(this.GetType(), "button", "<script type = 'text/javascript'>alert('Room no " + (sender as Button).Text + " selected');</script>");  
  30. }  
could you tell me instead of this 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'

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Paul Rajs
    0
    sorry friends i have done with this use the following query
     
    Button btn = (Button)sender;
    string name = btn.Text;
     
    thanks to all 
    0