asp.net keeps on calling a function in page_load - c#

I've a form (mainpage.aspx), on which there's a button called 'Cancel'. Here's the code for the button:
<asp:LinkButton ID="btnCancel" runat="server" Text="Cancel" CssClass="btn btn-default" OnClick="btnCancel_Click" />
and the called function
protected void btnCancel_Click(object sender, EventArgs e)
{
Response.Redirect("EventList.aspx");
}
So as you can see, what the button do is simple. Just go to another page. But it seems here that doesn't happen. Every time I click the Cancel button, first it will try to load the code
Response.Redirect("EventList.aspx");
but then somehow it will try to execute the codes below:
if (hdnEventId.Value != "" && hdnEventId.Value != "0")
{
LoadEvent();
}
I know this because I put a breakpoint on Response.Redirect("EventList.aspx"); and after that I step into the code above
btw the codes above are located inside
protected void Page_Load(object sender, EventArgs e)
inside the file mainpage.aspx.cs

That's because Page_Load is called on any postback and you're actually doing a postback by clicking Cancel. The solution is to wrap your non-postback specific code in a IsPostBack check:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (hdnEventId.Value != "" && hdnEventId.Value != "0")
{
LoadEvent();
}
}
}

Related

UserControl Textbox Always null (call from another UserControl)

First.ascx
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
public void View(object sender, EventArgs e)
{
string name = txtName.Text;
}
Second.ascx
<button type="button" runat="server" onserverclick="Button1_click">View</button>
protected void Button1_click(object sender, EventArgs e)
{
UserControl uc = (UserControl)LoadControl("First.ascx");
uc.View(sender, e);
}
Why "txtName" always null, if I create a Button on First.ascx it's normal, but if I call function from Second ascx always null value.
You should look postback. Because every usercontrols page_load run afeter postback. You can catch your text value after postback in page_load.
if (!IsPostBack)
{
//Catch your value
}

Use commandeventargs to a HTML Button control

I have a HTML control ie. Button. PFB Snippet
<button type="button" id="Button7" onserverclick="btnSave_OnClick"
runat="server" commandname="SaveNext">
Next
</button>
I am calling a same server side method from 2 different Button. I need to validate which button is clicked using CommandName in method.
Code behind:
protected void btnSave_OnClick(object sender, EventArgs e)
{
}
The problem is EventArgs e does not have CommandName . I changed method parameter to commandeventargs e which gives error as it requires EventArgs e
I am not preferring to change the HTML control to a asp:button.
Please suggest.
In the server-side event handler, you can retrieve the CommandName from the attributes of the button:
protected void btnSave_OnClick(object sender, EventArgs e)
{
HtmlButton btn = sender as HtmlButton;
string commandName = btn.Attributes["CommandName"];
...
}

onserverclick wont trigger the event

Nothing works, The button doesn't trigger the event
code of button:
<input type="button" id="regBtn" runat="server" onserverclick="RegClick"/>
script code:
<script runat="server">
void RegClick(object sender, EventArgs e)
{
regBtn.Value += "a";
}
</script>
If you use ASP.Net Web Form, you want to use Service Control whenever posting back to server. It will go through Life Cycle Events, and trigger the appropriate event correctly.
<asp:Button ID="SubmitButton" runat="server" Text="Submit" OnClick="SubmitButton_Click"/>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SubmitButton_Click(object sender, EventArgs e)
{
SubmitButton.Text += "a";
}

How to get Text property of TextBox after ItemCommand event

I have a TextBox control inside a panel and this panel is inside DataList ItemTemplate.
After firing the ItemCommand event, everything works fine except that the TextBox.Text property is always an empty string "" although there is some text in it.
I tried several ways but without success. I would really appreciate if someone can assist me with this. Simplified code is shown below.
Thank you!
ASPX page:
<asp:DataList ID="dlDataList" runat="server" onitemcommand="dlDataList_ItemCommand">
<ItemTemplate>
<asp:Panel ID="pnlReply" runat="server" Visible="False">
<asp:TextBox ID="txtTextBox" runat="server"></asp:TextBox><br />
<asp:LinkButton ID="lnkbtnSend" CommandName="Send" runat="server">Send</asp:LinkButton>
</asp:Panel><br />
<asp:LinkButton ID="OpenPanel" CommandName="OpenPanel" runat="server">Open panel</asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</asp:Content>
ASPX.CS Page code behind
protected void Page_Load(object sender, EventArgs e)
{
FillDataList();
}
private void FillDataList()
{
List<string> list = new List<string>();
list.Add("First");
list.Add("Second");
list.Add("Third");
dlDataList.DataSource = list;
dlDataList.DataBind();
}
protected void dlDataList_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "OpenPanel")
{
Panel pnlReply = (Panel)e.Item.FindControl("pnlReply");
pnlReply.Visible = true;
}
if (e.CommandName == "Send")
{
TextBox txtTextBox = (TextBox)e.Item.FindControl("txtTextBox");
//I tried this way also..
//TextBox txtTextBox = (TextBox)e.item.FindControl("pnlReady").FindControl("txtTextBox");
Label1.Text = txtTextBox.Text;
}
}
Please use IsPostBack in page load event. Without it your FillDataList(); is executing on every postback and resetting your DataList.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDataList();
}
}

how to Call cs function from html button

Html Button
<input id="Button1" type="button" value="button" runat="server"/>
.cs file:
public void display()
{
Response.Redirect("default.aspx");
}
How to call the display function which is in .cs file from html button click
Use a <asp:button> instead and just bind the click event in the designer.
This will generate an input in the actual html.
In your codebehind file use the following:
protected void Page_PreInit(object sender, EventArgs e)
{
Button1.ServerClick += display;
}
And change your display method signature to:
private void display(object sender, EventArgs e)
{
Response.Redirect("default.aspx");
}

Categories

Resources