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

StackOverflow https://stackoverflow.com/questions/12372825

  •  01-07-2021
  •  | 
  •  

質問

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.

役に立ちましたか?

解決

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

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top