Search code examples
c#asp.netautopostback

How do you grab the control that caused postback, if it is present within a ListView ItemTemplate


I am currently working on a project that involves creating a questionnaire from a list of questions in the database. I am using the ListView control with paging to display each question and it's choice of answers. The choices are RadioButtons with autopostback enabled.

When a radiobutton is clicked and causes postback, I want to grab the text associated with the radiobutton and insert it into another database table that records the responses for each question.Since the radiobutton is inside the listview control, I'm not sure how to handle postbacks for that.I need help in finding the radiobutton that caused postback. I'm new to ASP.Net, please help me out with this. Thanks.


Solution

  • RadioButton has a CheckedChanged event that you can use to grab the text you need.

    Example:

    <asp:RadioButton ID="radio1" runat="server"
      OnCheckedChanged="radio1_CheckedChanged" />
    
    void radio1_CheckedChanged(object sender, EventArgs e)
    {
       string text = ((RadioButton)sender).Text;
    }
    

    Or you just could use the control ID:

    void radio1_CheckedChanged(object sender, EventArgs e)
    {
       string text = radio1.Text;
    }