5

HI i have above html tag in my asp.net listview item template ,

<td> 
<asp:CheckBox runat="server" ID="chkStudentStatus"  Text='<%# GetStatusString(Eval("StudentStatus").ToString()) %>' CommandName='<%#Eval("StudentID")%>' OnCheckedChanged="chkStudentStatus_CheckedChanged" Checked='<%#Eval("StudentStatus") %>'  AutoPostBack="True" />
</td>

While check box value changed i was to get the Command Name value in the " chkStudentStatus_CheckedChanged " function

sangram parmar
  • 8,462
  • 2
  • 23
  • 47
Vinoth
  • 753
  • 3
  • 12
  • 23

2 Answers2

8

try this:

Short and simple

Refrence

your check box

<td> 
<asp:CheckBox runat="server" ID="chkStudentStatus"  Text='<%# GetStatusString(Eval("StudentStatus").ToString()) %>' CommandName='<%#Eval("StudentID")%>' OnCheckedChanged="chkStudentStatus_CheckedChanged" Checked='<%#Eval("StudentStatus") %>'  AutoPostBack="True" />
</td>

in code behind

protected void chkStudentStatus_CheckedChanged(object sender, EventArgs e)
{
    var chk = (CheckBox)sender;
    var studentID = chk.Attributes["CommandName"];


}

you can give any named attribute i.e. xyz='<%#Eval("StudentID")%>'

than in code behind

protected void chkStudentStatus_CheckedChanged(object sender, EventArgs e)
{
    var chk = (CheckBox)sender;
    var studentID = chk.Attributes["xyz"];


}
sangram parmar
  • 8,462
  • 2
  • 23
  • 47
  • This works but keep in mind the data added to the Attributes collection will get rendered in the wrapper tag. If you want to append the property to the check box itself, you have to use InputAttributes instead of Attributes – Tomas Beblar Jul 31 '20 at 23:10
-1
var item = (RepeaterItem) sender.NamingContainer;

change with:

var item = (RepeaterItem) chk.NamingContainer;
David Buck
  • 3,752
  • 35
  • 31
  • 35