I have a link on the page and i want when the user is pressing it, to evaluate a method where i check if the usser is logged in. If not, i want to show up a modal popup.
I know how to do the back-end checking and how to redirect the user but i have problems
with the link button:
<asp:LinkButton ID="LinkButton1" runat="server"
OnClientClick="redirectToWishList()">LinkButton</asp:LinkButton>
When i press it, it does a full postback instead evaluating the method where i show up the modal pop.
Do you have any workaround to not fire a full postback but check the method where i am doing the redirect?
Ps: The method which has to fire:
public void redirectToWishList(object sender, EventArgs e)
{
ASP.usercontrols_loginpopup_ascx loginUserControl = (ASP.usercontrols_loginpopup_ascx)UtilsStatic.FindControlRecursive(Page, "loginPopUp");
ModalPopupExtender modal = (ModalPopupExtender)loginUserControl.FindControl("loginPopUp");
modal.Show();
}
You need to call redirectToWishList as a function (including the parens): redirectToWishList()
<asp:LinkButton ID="LinkButton1" runat="server"
OnClientClick="redirectToWishList();return false;">LinkButton</asp:LinkButton>
Also, if you return false in your function, you do not need to in the tag.
UPDATE
I misunderstood. The OnClientClick is for invoking JavaScript, not running methods in your code-behind. You need to OnClick instead.
Do you return false from your redirectToWishList function?
You shouldn't need the return false in the LinkButton declaration.
Related
How to know which server side method is called on button click. I am new and everything in the code is wrapped. So on
<asp:Button ID="Apply" runat="server onClick = "Apply_Click" .. >
And on the Button click function it just has>>
Apply_Click(object sender, EventArgs e)
{
GetXML();
}
the GetXML method just generates the XML file.
But in click it actually validates the coupon entered and if it is correct than allows next page, else displays error. The GetXML() just prepares a XML file, it has no Stored Procedure call or any other method which may make this call of Validating the Coupon.
Can anyone tell me how to see which server side method is being called to validate the Coupon.
If it is a custom method than how to see its code.
If you don't want to execute validation with Apply_Click specify it:
<asp:Button ID="Apply" CausesValidation="false" runat="server
onClick = "Apply_Click" .. >
As for validation method, it can be any custom validator
<asp:CustomValidator OnServerValidate="magic_here" />
or custom javascript or Page_Load or Page_Init piece of code.
I have this hyperlink called “SEND” in a ASP page called Home and here it is:
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("Post_ID", "~/RCA.aspx?Post_ID={0}") %>'
Text="SEND"></asp:HyperLink>
</ItemTemplate>
when the user clicks the hyperlink it goes to another page called RCA and in this page there is a Button and here it is the code:
<asp:Button ID="btnRCA" runat="server" onclick="Button1_Click"
Text="Assign RCA" Width="147px" />
so I want this button to be visible only when clicked the hyperlink in the HOME page. I am planning to have another button or control in the RCA page that will make it invisible when clicked or before someone leaves the page they have to make it invisible the Button by clicking some other control. can someone help me with this? thanks
Use a QueryString parameter.
Home.aspx
//When linked to RCA.aspx from Home.aspx, a parameter called ShowButton=1 is included
//in the URL.
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("Post_ID", "~/RCA.aspx?Post_ID={0}&ShowButton=1") %>'
Text="SEND"></asp:HyperLink>
RCA.aspx
//By default, since you want the button to NOT appear for all incoming traffic EXCEPT
//that which came from Home.aspx, the button's Visible property is set to false.
<asp:Button ID="btnRCA" runat="server" onclick="Button1_Click"
Text="Assign RCA" Width="147px" Visible="false" />
RCA.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
//If the querystring has a parameter called ShowButton and it's equal to "1",
//then set the button to Visible = true.
//Else, do nothing, keeping the button in it's default, Visible=false state.
//By putting this in an "IsPostback == false" check, you can guarantee that this will
//only happen on first page_load, and won't be triggered again even if you do other
//actions in the page that cause Postback
//For example, if you don't use this !IsPostback check, and you end up creating some
//new function that causes the button to be hidden again, but then you make a
//selection from a dropdown list that causes postback, you will trigger the call to
//make the button Visible again, even though that's probably what you don't want at
//this point, since your other new function set it to Visible = false.
if (!IsPostback)
{
if (Request.QueryString["ShowButton"] == "1")
{
RCAbtn.Visible = true;
}
if (Request.QueryString["Post_ID"] != null)
{
//do whatever you need to with the post ID
}
}
}
SomeOtherPage.aspx.cs
Response.Redirect("RCA.aspx?Post_ID=1234"); //button will be invisible
And then let's say later that you want to re-direct from some other page and have the button be visible, like the redirect from Home:
Response.Redirect("RCA.aspx?Post_ID=1234&ShowButton=1"); //button will be visible
If you don't like cluttering up your URL or you feel that it looks tacky to have what you are doing so plainly available to the user's eyes, you don't necessarily need to use "ShowButton". You could say ?Post_ID=1234&fkai3jfkjhsadf=1, and then check your query string for "fkai3jfkjhsadf". I like to do that sometimes because then from the users point of view, it makes me look like I'm doing something really technical and encrypted, and not just passing around a bunch of basic instructions in plain English :) Downside there is you need keep track of your own query string parameters.
Edit:
If you want to get the URL with only the Post_ID and nothing else, you can do this:
string currenturl = Request.Url.ToString(); //get the current URL
string urlToSend = currenturl.Substring(0, currenturl.IndexOf("?")); //cut off the QueryString entirely
urlToSend += "?Post_ID=" + Request.QueryString["Post_ID"]; //re-append the Post_ID
Be aware that your call to Substring will cause an exception if the URL doesn't have a QueryString, so please patch that up in whatever way works best for you (try/catch, etc.).
After that, you should just be able to use the "urlToSend" string in your mailMessage.Body.
on your second page in your page_load, try this:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["Post_ID"] != null)
{
btnRca.Visible = true;
}
}
I don't know how you want to handle the visibility of this button in other cases, but this should answer your particular question.
I have an asp.net linkButton (or imageButton) control in my profile.aspx page. I'am checking Request.Querystring("id") in the page below in the code behind.
http: //localhost:42932/profile.aspx?id=1
When I first load the profile page it is not posted back. It is ok!. When I go to another users profile (the same page just the query string is different) using imageButton control with the adddress;
http: //localhost:42932/profile.aspx?id=2
it is posted back. I dont want it to be posted back. But if I go to this page with a regular html input element like
a href = "http: //localhost:42932/profile.aspx?id=2"
it is not posted back. So I want the image button behave like an html input element.
Here is my imageButton;
ASPX:
<asp:ImageButton ID="imgProfile" ImageUrl="images/site/profile1.png" runat="server"/>
.CS
imgProfile.PostBackUrl = "profile.aspx?id=" + Session["userID"];
Edit:
if (!IsPostBack)
{
Session["order"] = 0;
}
This control is in the page load. So it should be !postback with state I mentioned above. Because all the other functions are working when Session["order"] = 0
Make use of OnCLientClick instead of OnClick, so that you only run client side code. Then, sepcify that you return false;
i.e.
<asp:ImageButton ID="imgProfile" ImageUrl="images/site/profile1.png" runat="server" OnClientClick="return false;" />
But, why use a server control, when this can be done with a normal <img .. html control?
Rather than specifying a PostBackUrl I would recommend using Response.Redirect() in the button click event handler:
public void imgProfile_Click(object sender, eventArgs e){
Response.Redirect("profile.aspx?id=" + Session["userID"]);
}
Or alternatively, just use a Hyperlink control and set the NavigateUrl property during Page_Load:
<asp:HyperLink ID="imgProfile" runat="server"><img src="images/site/profile1.png" /></asp:Hyperlink>
imgProfile.NavigateUrl = "profile.aspx?id=" + Session["userID"];
i am using validators for validation and on linkbutton i am diaplaying popup.
my problem is i want to disable linkbutton means until page is validated means the popup should not be displayed till the page gets validated
<asp:LinkButton ID="LinkButton1" runat="server" CssClass="addProduct-disable" Enabled ="false"
Text="Assign Filter Criteria" CausesValidation="true"></asp:LinkButton>
Try to validate client-side, if possible, via AJAX-Methods.
You may consider using the jQuery event.preventDefault() method.
$('[id$="LinkButton1"]').click(function(event) {
if(! valdiateYourPage() ) {
event.preventDefault();
// display validation errors or something
}
else {
//proceed as normal
}
});
Put your page validation logic in the valdiateYourPage() javascript method. If it valdiates, then process as normal, if not then preventDefault() will stop any further event execution by your LinkButton.
i have a form on a particular ASPX page that has custom js validation. Also on this form is a navigation menu in the header.
The navigation menu items are built programatically and are initiated by __doPostBack calls which obviously submits the form.
The problem i am having is that the form itself has code something like the following
onsubmit='return validateForm()'
and if the form has not been filled out then the form cant submit. This in itself is not a problem unless a user goes to the form but decides to navigate away.
When this happens the validateForm function fails.
Does anyone have a workaround for this issue?
NB: On my nav links i have already set CausesValidation="False"
This is the markup:
<div id="divNavigate" class="absolute_topleft">
<asp:LinkButton ID="linkGoHome" runat="server" OnClick="linkGoHome_Click" CausesValidation="false" CssClass="xxx">text.</asp:LinkButton>
</div>
This is the handler:
protected void linkGoHome_Click(object sender, EventArgs e)
{
Response.Redirect("xxxxx");
}
This is the validation function:
function validateTextField(field) {
var fieldElement = document.getElementById(field);
var validated = false;
if (fieldElement.value.length > 0) {
validated = true;
}
return validated;
}
And this is how its called:
<form id="formLogin" runat="server" onsubmit="return validateTextField(field1)">
Are your navigation links proper asp.net server controls? If they are setting CausesValidation="False" should prevent them from causing validation. Can you post the code where you are dynamically adding them to the page, the most liekly explanation is the the CausesValidation property is not being set corectly.