日期:2014-05-18  浏览次数:20524 次

datalist中button按钮的OnClick事件
HTML代码:
<asp:datalist   id= "dltStudents "   runat= "server "   Width= "1000 "   BackColor= "#ccccff "   BorderColor= "black "
ShowFooter= "false "   CellPadding= "3 "   CellSpacing= "0 "   Font-Name= "宋体 "   Font-Size= "8pt "   HeaderStyle-BackColor= "#aaaadd ">
<SelectedItemStyle   Font-Size= "X-Small "> </SelectedItemStyle>
<ItemStyle   Font-Size= "X-Small "> </ItemStyle>
<ItemTemplate>
<table>
<tr>
<td   width= "120 "> <%#   DataBinder.Eval(Container.DataItem, "StudentID ")   %> </td>
<td   width= "150 ">
<%#   DataBinder.Eval(Container.DataItem, "StudentName ")   %>
</td>
<td   width= "80 "> <%#   DataBinder.Eval(Container.DataItem, "StudentSex ")   %> </td>
<td   width= "100 "> <%#   DataBinder.Eval(Container.DataItem, "NativePlace ")   %> </td>
<td   width= "130 "> <%#   DataBinder.Eval(Container.DataItem, "StudentTel ")   %> </td>
<td   width= "120 "> <a   href= "Classes.aspx "> <%#   DataBinder.Eval(Container.DataItem, "ClassName ")   %> </a> </td>
<td   width= "120 "> <a   href= "Courses.aspx "> <%#   DataBinder.Eval(Container.DataItem, "CourseName ")   %> </a> </td>
<td   width= "120 "> <a   href= "Teachers.aspx "> <%#   DataBinder.Eval(Container.DataItem, "TeacherName ")   %> </a> </td>
<td   width= "50 ">
<asp:Button   text= "Detail "   Runat= "server "   ID= "btnDetail "   OnClick= "btnDetail_OnClick "> </asp:Button> </td>
</table>
</ItemTemplate>
</asp:datalist>
C#中btnDetail_OnClick代码:
protected   void   btnDetail_OnClick(object   sender,   System.EventArgs   e)
{
string   url2   =   dltStudents.Items[0].FindControl( "lblStudentID ").ToString();
string   url;
url= "2.aspx?name= "   +   url2;
Response.Redirect(url);  
}
我想当点击某行中button按钮的时候   在btnDetail_OnClick   怎么获取这个行的lblStudentID的数据?
希望高手指点一下!谢谢了!
最好能有具体的代码!

------解决方案--------------------
定义按钮的commandName
然后在DataList1_ItemCommand事件中判断是否点击的按钮的名字是所定义的commandName 然后操作
------解决方案--------------------
Button按钮有一个属性是CommandName,给它负一个值 例:CommandName等于btn
DataList的DataKeyField等于StudentID
然后在DataList1_ItemCommand事件里判断一下
string url2 = Datalist1.DataKeys[e.Item.ItemIndex].ToString();
if(e.CommandName == "btn ")
{
string url;
url= "2.aspx?name= " + url2;
Response.Redirect(url);
}