Check box inside repeater , How to get command name value in the check changed function

丶灬走出姿态 提交于 2019-12-19 21:53:53

问题


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


回答1:


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"];


}



回答2:


Use the CommandArgument to store the StudentID instead of the CommandName. However, since you want to handle the CheckedChangedEvent there is no CommandName or CommandArgument.

I would use a HiddenField to store the student-id. Then you can access this control from the CheckedChangedEvent in the following way:

protected void chkStudentStatus_CheckedChanged(Object sender, EventArgs e)
{
    var chk = (CheckBox) sender;
    var item = (RepeaterItem) sender.NamingContainer;
    var hiddenIdControl = (HiddenField)item.FindControl("hiddenIdControl");
    int studentID = int.Parse(hiddenIdControl.Value);
}


来源:https://stackoverflow.com/questions/18439197/check-box-inside-repeater-how-to-get-command-name-value-in-the-check-changed-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!