如何在页面加载时获取按钮单击事件? [英] how to get button click event on page load ?
问题描述
我希望我的按钮点击页面加载
如果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();
}
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 backstring 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屋!