问题
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