如何在页面加载时获取按钮单击事件? [英] how to get button click event on page load ?

查看:141
本文介绍了如何在页面加载时获取按钮单击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的按钮点击页面加载

如果ispostback = true



我该怎么做?



plz help

提前感谢

解决方案

你可以得到控制ID导致页面回发

  string  postbackControlID = Page.Request.Params [  __ EVENTTARGET]; 
控制postbackControl = Page.FindControl(postbackControlID);



然后验证postbackControl是否是你的按钮


试试这个代码。









< asp :button id =btn_submitrunat =servertext =submitxmlns:asp =#unknown>

< asp:label id =lblrunat =server xmlns:asp =#unknown>











protected void Page_Load(object sender,EventArgs e)

{

if(Page.IsPostBack)

{

btn_submit.Click + = new EventHandler(btn_submit_Click);



}

} < br $>


void btn_submit_Click(object sender,EventArgs e)

{

lbl.Text =hi;

//抛出新的NotImplementedException();

}


只需像这样调用page_load中的事件..





  protected   void  Page_Load( object  sender,EventArgs e)
{
if (!IsPostBack)
{
btnClieck_Click( new object (), new EventArgs());
}
}
受保护 void btnClieck_Click( object sender,EventArgs e)
{
lblResultShow.Text = IM Clicked !!;
}


i want my button click on page load
where if ispostback=true

what me should to do?

plz help
thanks in advance

解决方案

you can get the control id that is causing the page post back

string postbackControlID = Page.Request.Params["__EVENTTARGET"];
Control postbackControl = Page.FindControl(postbackControlID);


then verify if the postbackControl is your button or not


try this code.




<asp:button id="btn_submit" runat="server" text="submit" xmlns:asp="#unknown">
<asp:label id="lbl" runat="server" xmlns:asp="#unknown">






protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
btn_submit.Click += new EventHandler(btn_submit_Click);

}
}

void btn_submit_Click(object sender, EventArgs e)
{
lbl.Text = "hi";
//throw new NotImplementedException();
}


Just call the Event in page_load like this..


protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        btnClieck_Click(new object(), new EventArgs());
    }
}
protected void btnClieck_Click(object sender, EventArgs e)
{
    lblResultShow.Text = "I M Clicked!!";
}


这篇关于如何在页面加载时获取按钮单击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆