i am trying to pass a value from client side and recieve it on server side on click of hyperlink.but it should not be called everytime on page load ,currently i am trying like this but it is reloaded every time when page load.so i want it to be called one time.
on click event of hyperlink TDC_No passing value to tdc.aspx
$.each(customers, function () {
var customer = $(this);
$("td", row).eq(0).find("a").text($(this).find("TDC_NO").text());
$("td", row).eq(0).find("a").attr("href", "TDC.aspx?Id=" + $(this).find("TDC_NO").text());
});
calling at page load side
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["Id"] != null)
{
TW12HVGI();
}
}
Any idea would be appreciated.
Not exactly sure what you're aiming for here. Do you mean that you pass the value - and the page loads but then subsequent actions on that page cause the TW12 function to execute again?
If so you are probably looking for a check on IsPostBack:
protected void Page_Load(object sender, EventArgs e){
if (!IsPostBack){
//if its not a post back - check for query string and run the TW12 function
if (Request.QueryString["Id"] != null)
TW12HVGI();
}
}
https://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback(v=vs.110).aspx
If you want to receive it on server side you will need to post back it using AJAX. On click of hyperlink send an ajax call and data to be saved along with it and then you will receive what ever you sent in action specified in ajax call.
Related
I have created server created forms using Telerik AJAX controls with a submit button that does a post (no click event). My forms use validators which work fine except they happen on the next post after the field validation error.
If I make an email field empty which the code logic catches and returns an error. Submit again and the email required field validator works to catch the error before the code. Now put valid data and submit and the last empty field validator error with appears again. Submit the good data a second time and it updates.
Also, if I produce my own validator error then the validators work fine. I have validator code for required checkboxes. If a missing check happens I call
ValidatorError.Display(Page, message);
which causes the subsequent
Page.Validate(FormEntryBase.VALIDATION_GROUP);
to properly validate required fields in current submit.
protected void Page_Load(object sender, EventArgs e) {
localValidationError = theForm.CustomValidateForm(Request.Form);
if (localValidationError) {
validateErrorList = form.validateErrorList;
// get local valiation error put into validation summary error list
if (validateErrorList != null) {
foreach (string message in validateErrorList) {
ValidatorError.Display(Page, message);
}
}
}
Page.Validate(FormEntryBase.VALIDATION_GROUP);
if (!Page.IsValid ) {
return;
}
Validator errors the code does not catch are not caught on an initial submit which lets bad data in.
I have another web form that uses the click method and only uses Page.IsValid which works fine.
Very strange behaviour, some sort life cycle problem probably.
With some luck, I figured out a solution by adding a click event in code. To add a click event, first I added the RadButton to my form creation code be accessed in the aspx.cs.
RadButton button = new RadButton();
button.ValidateRequestMode = ValidateRequestMode.Enabled;
button.ValidationGroup = VALIDATION_GROUP;
button.UseSubmitBehavior = true;
theForm.updateButton = button;
In the aspx.cs Page_Load I added the click method to the button.
protected void Page_Load(object sender, EventArgs e) {
...
theForm = group.GetJoinForm();
theForm.updateButton.Click += new EventHandler(UpdateButton_Click);
...
void UpdateButton_Click(object sender, EventArgs e) {
Page.Validate(FormEntryBase.VALIDATION_GROUP);
if (!Page.IsValid) {
return;
}
... most of page load code moved here
These three steps fixed my validate delay problems and all my forms now validate quite well.
i have 2 server side asp.net buttons , i need to automate the buttons clicks.
i.e. After page_load, i need to click button1 and after its results are shown on the page, wait for 10 seconds and click button2 .
i tried the following sample code
protected void Page_Load(object sender, EventArgs e)
{
Button1_Click(Button1, null);
Thread.Sleep(10000);
Button2_Click(Button2, null);
}
protected void Button1_Click(object sender, EventArgs e)
{
changeLabel.Text = "Button1";
}
protected void Button2_Click(object sender, EventArgs e)
{
changeLabel.Text = "Button2";
}
}
i had 2 obeservations (maybe useful):
always Button2_Click(Button2, null); event is the latest when the page is fully loaded (which is obvious).
Page_Load(object sender, EventArgs e) doesnot
hit atall when programatically clicked the button.
Any idea how to achieve the solution.
What you are doing in your example is to call the handlers of button1 and button2's click events, that is not the same as having the user click the buttons, and for the form to postback to the server.
If you want the buttons to click them selves and having the post back to the server you need to add javascript that clicks the buttons for you.
If you want a javascript which hits a button for you after X seconds, i would do something like this:
in your aspx page:
<asp:button ID="Button1" runat="server" ClientIDMode="Static" OnClick="Button1_Click">
</asp:button>
in your javascript files or some block on your page:
<script type="text/javascript">
setTimeout(function(){
document.getElementById("Button1").click();
}, 10*1000); // 10 seconds
</script>
Important to note here is that you have to either set ClientIDMode="Static" on your button, otherwise it might have a very obscure name if you are using master pages, or you can do:
<script type="text/javascript">
setTimeout(function(){
document.getElementById("<%= Button1.ClientID %>").click();
}, 10*1000); // 10 seconds
</script>
if you have the javascript in your .aspx file rather then its own .js file.
ps: if you do Thread.Sleep(X) in an aspx page, you will only make the users browser wait for the X milliseconds more for the page to load, code run before the sleep will not be submitet to the clients browser in the way i think you want it to do.
Button_Click is server event, when invoked from client, browser post the relavent data to server and request of new page content, at that time Page_load is invoked.
if you want to do some thing, encapsulate action to some method and call that method in pre-render. Or other wise, use JavaScript.
protected void Page_PreRender(object sender, EventArgs e)
{
UpdateButton1()
Thread.Sleep(10000); // no need to put sleep
UpdateButton2();
}
protected void Button1_Click(object sender, EventArgs e)
{
UpdateButton1();
}
protected void UpdateButton1()
{
changeLabel.Text = "Button1";
}
I have a C# web page viewer working. What I need to do is when a button is clicked within that webpage, it changes the URL that begins with a certain string, say "xyz". I need to know how I could detect this change.
In android I simply used shouldOverrideURlLoading and had an if statement but the only URL I can retrieve is the original one I pass to start the web view.
is there a way to call DocumnetedCompleted after each new screen..There are appox. 2 button presses that get me to the screen with the important button
If you are using a WebBrowser control, you can use the Navigating event to handle when the site navigates to another url.
From there you can use the WebBrowserNavigatingEventArgs to retrieve the new URL and to stop it if you want or change the destination URL.
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if (e.Url.AbsoluteUri.Contains("something")) {
//stais in the current page
e.Cancel = true;
//aditionally you can navigate to another URL (though it will fire this event again)
webBrowser1.Navigate(e.Url.AbsoluteUri.Replace("something", "empty"));
} else {
//continue
}
}
I think you need something like this
private void change_Url () {
var URL = Request.Url.ToString(); // get the URL
// if you meant to appened the text, then
URL = URL + "abc";
}
I have a "Pay" button where we are starting to accept credit card. I need to call a Javascript function from the server side click event. I tried with Response.Write as below but that does not trigger my function that is defined in the separate .js file. What else can I do?
protected void btmMakePayment_Click(object sender, EventArgs e)
{
if (user selected credit card)
{
Response.Write("<script language='javascript' type='text/javascript'>OpenPayPalDialog();</script>");
}
else
{
continue with the current server side logic
}
}
Thank You in advance.
Use ClientScriptManager.RegisterStartupScript, which registers a block of JavaScript to execute when the page loads.
You can use ScriptManager.RegisterClientScriptBlock to invoke JS event:
ScriptManager.RegisterClientScriptBlock(this, typeof(this), "JSKey", "JSFunctionName(<param>);", true);
I want to share the currentTab variable which exists on the C# server side with JavaScript. Here is my code:
C#:
public int currentTab = 1;
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "showTab(" + currentTab + ");", true);
}
JavaScript:
var currentTab = "<%=currentTab%>";
function showTab(index)
{
currentTab = index;
// Show tab at (index)
}
I used this approach to get the current tab again on PostBack. However, currentTab on C# is remains 1 after PostBack. How can I solve this issue?
You can write the index from your javascript function, to a hiddenfield, and then read that on postback.
In your code, you check the hiddenfield, if your page is postback.
Like so:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
currentTab = Int32.Parse(HiddenTabValue.Value);
}
}
Have a server side hidden field to hold this piece of information.
You can access the field through javascript and as a server side control the value will be available server side.
You need to use some server control to send value back to the server (i.e. asp:HiddenField) or use query string to set the tab index there.