OnServerClick的按钮不工作 [英] OnServerClick for button not working
问题描述
我有一个HTML按钮,我想有一个服务器端的click事件,但是当点击按钮时,它的不触发和页面做了回发。按钮是在的ItemTemplate
为< ASP:ListView控件ID =usersListView/>
呈现一个的信息表。
ASPX
<按钮=服务器ID =删除级=BTN BTN-迷你称号=删除OnServerClick =delete_Onclick>< /按钮>
aspx.cs
保护无效delete_Onclick(对象发件人,EventArgs的发送){
ListViewItem的的ListViewItem =(ListViewItem的)((按钮)发送方).NamingContainer;
如果(ListViewItem的!= NULL){
Membership.DeleteUser(((标签)listViewItem.FindControl(username的))文本。);
}
}
我的猜测是,它不会喜欢这个工作,因为摆在项模板嵌套控件引发的事件而应通过ListView的 ItemCommand处理
事件。
对于这个工作,那么,你应该设置按钮的的CommandName
和 CommandArgument
和ListView的处理特定值 ItemCommand
。
不过,如果我记得正确地将中, HtmlInputButton
没有的CommandName
和 CommandArgument
属性。相反,使用 ASP:按钮
< ASP:按钮的ID =删除=服务器的CommandName =东西CommandArgument =somethingelse/>
和处理ListView的itemcommand:
保护无效usersListView_ItemCommand(对象发件人,ListViewCommandEventArgs E)
{
开关(e.CommandName){
案东西:
//这是你处理按钮点击
}
}
注意 CommandArgument
通常绑定到特定商品的价值(ID也许),使服务器端处理程序中,你可以precisely确定准确的被点击键:
< ASP:按钮的ID =删除=服务器的CommandName =东西CommandArgument =<%#的eval(ID)%>中/> 保护无效usersListView_ItemCommand(对象发件人,ListViewCommandEventArgs E)
{
开关(e.CommandName){
案东西:
//这是你处理按钮点击
VAR的itemid = e.CommandArgument;
}
}
I have an html button and I'd like to have a server side click event but it's not firing when the button is clicked and the page does a postback. The button is inside the ItemTemplate
for an <asp:ListView id="usersListView"/>
that renders a table of information.
aspx
<button runat="server" id="delete" class="btn btn-mini" title="Delete" OnServerClick="delete_Onclick"></button>
aspx.cs
protected void delete_Onclick(object sender, EventArgs e) {
ListViewItem listViewItem = (ListViewItem)((Button)sender).NamingContainer;
if(listViewItem != null) {
Membership.DeleteUser(((Label) listViewItem.FindControl("userName")).Text);
}
}
My guess is that it will not work like this because events raised by nested controls placed in item templates should rather be handled by ListView's ItemCommand
event.
For this to work then, you should set button's CommandName
and CommandArgument
and handle specific values in the listview's ItemCommand
.
However, if I remember correcly, the HtmlInputButton
does not have CommandName
and CommandArgument
properties. Instead, use asp:Button
<asp:Button id="delete" runat="server" CommandName="something" CommandArgument="somethingelse" />
and handle the listview's itemcommand:
protected void usersListView_ItemCommand( object sender, ListViewCommandEventArgs e )
{
switch ( e.CommandName ) {
case "something" :
// this is where you handle the button click
}
}
Note that CommandArgument
is usually bound to an item-specific value (id perhaps) so that inside the server-side handler you can precisely identify the exact clicked button:
<asp:Button id="delete" runat="server" CommandName="something" CommandArgument="<%# Eval( "id" ) %>" />
protected void usersListView_ItemCommand( object sender, ListViewCommandEventArgs e )
{
switch ( e.CommandName ) {
case "something" :
// this is where you handle the button click
var itemid = e.CommandArgument;
}
}
这篇关于OnServerClick的按钮不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!