Click event not being called for HTML submit button - c#

I need to add button in user control. When the button is clicked url should be captured and stored in a session. When I click the button, server click event is not being called. I am in confusion why it is not being called. Did I made anything wrong here? Please suggest.
I have the following code:
ascx:
<input type="submit" id="QuickViewbutton" value="Click to add this page to Quick View"
runat="server" onserverclick="addQuickView" clientidmode="Static"/>
ascx.cs:
In page load:
QuickViewbutton.ServerClick += new System.EventHandler(this.addQuickView);
In protected void addQuickView(Object sender, EventArgs e) function I have written the code for fetching url:
string strUrl = HttpContext.Current.Request.Url.AbsoluteUri;
Session["QuickViewUrl"] = strUrl;

Related

Button click only fires when IP in the URL not computer name

I have an asp.net webform. When I browse to the URL using the computer name ie: http://somecomputer/mypage.aspx the button click events do NOT fire. Other events do like selected index changed or text changed but the button click does not.
If I navigate to the page by IP, ie: http://192.168.100.1/mypage.aspx, then the button click fires and everything works as expected. Why is this and how can I fix it?
here is the button click html:
<div class="li_div">
<asp:Button ID="btnInventoryRpt" runat="server"
Text="Load Report" OnClick="btnInventoryRpt_Click" CausesValidation="false" UseSubmitBehavior="False" />
</div>
Here is the code behind:
protected void btnInventoryRpt_Click(object sender, EventArgs e)
{
load_inventory_report();
}

asp.net search button just refreshing page

Im trying to create a search button on my website on my homepage which is an aspx page but when I click search all it does is refresh the page instead of preforming the query, any help would be appreciated
heres the code for my index.aspx and the code for my index.aspx.cs page
<asp:TextBox ID="searchtitle" runat="server"></asp:TextBox>
<asp:Button ID="searchitems" runat="server" Text="Search" />
protected void searchitems_Click(object sender, EventArgs e)
{
String stext = searchtitle.Text;
Response.Redirect("search.aspx?searchquery=" + stext);
}
The button is not working because you didnt call the event on the button
<asp:Button ID="searchitems" runat="server" OnClick="searchitems_Click" Text="Search" />
add OnClick attribute with name of the event you want to call and in the given code the event you want to call is searchitems_Click.
definitely when you will click the searchitems page will reload because function for that click is defined on server side.. what you have to do is on the pageload look for the QueryString and get the value of ["searchquery"] then manipulate it as you want..
if you want to not refresh the page use Ajax then...

Website doesn't see newest session values until I refresh page, but refreshing resets values of controls

My ASP.NET website doesn't see newest Session values. When my form is being sent to the server after clicking the button, the result sent back to browser depends of Session values. I process them in Page_Load method. It doesn't see the last change od Session values, but one before last. It looks like button's event handlers are executed before page's event handles but I'm not sure if it is like that.
I tried to do my code in other methods like Page_PreInit etc, but it's still the same like that. The only thing that works for me is refreshing the page: Response.Redirect(Request.Url.AbsoluteUri); after any change of any Session value, but it resets values of all controls, which I want to be the same as before. Is there a better solution?
Example: when It runs first time, the label's text is "Click the button", but when I click any of the buttons one time, nothing happens. When I click any of the button second time, label's text is the value of first click (even if I click A and then B, value changes to A after clicking B).
form:
<form id="form1" runat="server">
<div>
<p>
<asp:Label ID="Label1" runat="server" />
</p>
<p>
<asp:Button ID="Button1" runat="server" Text="A" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="B" onclick="Button2_Click" />
</p>
<p>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</p>
</div>
</form>
Event handlers:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Session["Letter"].ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["Letter"] = "A";
}
protected void Button2_Click(object sender, EventArgs e)
{
Session["Letter"] = "B";
}
Global method:
void Session_Start(object sender, EventArgs e)
{
Session["Letter"] = "Click the button";
}
Without your code its very difficult, but is sounds like you are setting a Session collection value in the code of a button's click event( or some other control/event ), and expecting it to be in the SESSION collection during the Page_Load event.
It doesn't work like that - When the page request comes in, Page_Load happens before the control's events.
Instead of Page_Load use Page_PreRender this event occurs before the page is prepared to be sent back to the client.
Your addition of the code confirms the above.
Normally I wouldn't use Session_Start to initialize stuff like this, use Page_Load and IsPostBack property
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack){
Label1.Text = "Click the button";
}
}

Fire html button using .cs page

i need to fire html buttom after the end of my asp.net c# code
<asp:Button ID="Button3" runat="server"
Text="Button" onclick="Button3_Click" />
<input id="Button2" type="button" value="button" data-type="error" class="growl-type2" runat="server" />
and cs code is
protected void Button3_Click(object sender, EventArgs e)
{
----
i need to fire button2( automatically) in this position
}
Button clicks are just handled events. To fire a button click you simply need to raise the correct event.
You'll need something like: Button2_Click(sender, e)
Alternatively, you can inject some Javascript into the page, which reacts directly to the click of button3 or is caused by the click of button3 and causes the click of button2 through something like: document.getElementById('Button2').click()

asp.net Button.Click event not firing

I am having an ASP.WebSite in which I have added some form controls dynamically on aspx.cs using
form1.Controls.Add( " Object of Control ");
Also a have already added a button on form in aspx file using
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Add" UseSubmitBehavior="false" />
now when I run the program it converts the Button1 into an HTML submit button as below
<input type="button" name="Button1" value="Add" onclick="javascript:__doPostBack('Button1','')" id="Button1" />
and the form tag becomes
<form name="form1" method="post" action="Add1.aspx" id="form1" enctype="multipart/form-data">
when I click the Button1 it submits the form instead of calling the function on Code Behind.
How am I able to call the method Button1_Click specified on OnClick event of Button1?
Please Help.
Have you implemented a click-event handler in codebehind?
protected void Button1_Click(Object sender, EventArgs e)
{
Button btn = (Button)sender;
}
Button.Click Event
I don't know how this question is related to your dynamic control question since Button1 is added declaratively.
I shoud have added the button control dynamically as follow:
b1.ID = "EDIT";
b1.Text = "Add";
b1.Click +=new System.EventHandler(this.Button1_Click);
b1.Style["Position"] = "Absolute";
b1.Style["Top"] = "10px";
b1.Style["Left"] = "20px";
form1.Controls.Add(b1);
It is now calling the function Button1_Click on the click event of Button1.
I this case I have to create the function Button1_Click manually i.e. not just double clicking on the design page.
the controls you are adding dynamically, you have to add it on page load.
Then it will work fine.

Categories

Resources