Ashwin
Ashwin

Reputation: 82

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.

Upvotes: 0

Views: 411

Answers (2)

Alessandro
Alessandro

Reputation: 3760

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;
}

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460238

You can use the sender argument of an event-handler. For example in the CheckBox' CheckedChanged-event:

void Check_Clicked(Object sender, EventArgs e) 
{
    var checkBox = (CheckBox) sender;
    String text = checkBox.Text;
}

That works with every event, you can always get a reference to the control that raised that event via sender.

Upvotes: 2

Related Questions