Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > .NET Framework 2.0
|
.NET Framework 2.0 For discussion of the Microsoft .NET Framework 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 2.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 22nd, 2006, 06:19 AM
Authorized User
 
Join Date: Jul 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to disable PostBack for a button control?

I have a button control in a form in asp.net file, and I want its PostBack property to be disabled, so when the user clicks it no post back happens.

You must be saying "then why using a button control?". The answer is I want some functions to be invoked when this button is clicked to make some calculations and other functionalities.

The exact situation is that I have a poll in the page with a submit button, when a user clickes the button no post back happens, but a pop up window opens showing the poll results. I can't remove the runat="server" because I want the users votes to be inserted in the database, so the button must remain a server control to be able to act with it within the code.


 
Old October 22nd, 2006, 06:41 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

I am not sure I understand what you want. If you want a simple client side button, just use <input type="button" /> that can fire some JavaScript to open a new window.

But apparently, that's not what you want is it? Can you explain what you mean with this:
Quote:
quote:I can't remove the runat="server" because I want the users votes to be inserted in the database, so the button must remain a server control to be able to act with it within the code.
This seems to be a contradiction with your request. You don't want a post back yet you want server side code to fire????

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old October 22nd, 2006, 07:37 AM
Authorized User
 
Join Date: Jul 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Imar
You don't want a post back yet you want server side code to fire????
You got what I mean, that is exactly what I want!

Though this looks strange, but the situation I mentioned above requires that.

As I said there is a function in the behind code (which stores the vote in the database) that I want to invoke when the button control is clicked, but in the same time I don't want the page to be posted back. The solution I found is using a simple html button as you said, but in this case how can I interact with the click event in the code? this can't be done unless the button is a server control, which will make the page post back!


 
Old October 22nd, 2006, 07:50 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
quote:this can't be done unless the button is a server control, which will make the page post back!
Exactly. Server side code is only triggered when the page first loads, or when a post back is raised. Without a postback, you can't run server side code like a Click event. If you think about it, it makes sense. Because the button posts back to the server, it raises the Click event on the server.

Depending on what you're trying to accomplish, there are a few waws around this:

a) Add additional (client side) code to the onclick handler of the button. That way, you can fire client side code, and after that the postback will occur.

b) Look into Ajax / Atlas. With Ajax you can still submit to the server, without a full page refresh.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old October 22nd, 2006, 08:24 AM
Authorized User
 
Join Date: Jul 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ofcourse Ajax is a good solution, but unfortunately I don't know much about Ajax yet.

There is another way to do it, that is invoking that function I want in the target page that will be opened in the pop up window. I want a pop up window to be opened when the button is clicked, so I must pass parameters in the url for the VoteID and ChoiseID to the target page and then do what I want to do there.

Now there is another problem: How can I pass these parameters in the url? I have those values in the behined code, I have the VoteID as a variable in C# code, and can get the selected choise from RadioButtonList.SelectedIndex, but how I can pass these values in the url?

here is the input button with the javascript function

<input type="button" value="vote" onclick="url1='target.aspx?voteid='+*+'&choiceid=' +*+';window.open(url1,null,'height=250, width=250');" />

I want to replace the * with the values from the C# behined code. Is that possible?

 
Old October 22nd, 2006, 08:30 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You can use an HtmlControl button type, with a runat="server" attribute. That way, you get a client side button that you can still access in server side code. E.g.:

<input id="Button1" type="button" value="button" runat="server" />

In code behind, you can set the onclick handler:

Button1.Attributes["onclick"] = "alert('Hello World');";

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old October 22nd, 2006, 04:21 PM
Authorized User
 
Join Date: Jul 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, that is realy a good solution, I tried it and it worked fine.

I also found another one by using single value data binding. I declared the variables and gave them a value in the code, then binding them using this.DataBind(), then using the variables in the aspx file in the format <%# variable %>.

But after that I faced another problem. As I mentioned I have a poll that I want the user to choose from it then press the button. The parameters I want to pass to the target page are the vote id and choise id. There is no problem in the vote id, but the problem apears when the user select another button in the RadioButtonList control. I want the selected index to be passed to the target page, but the value which is always passed is the default value specified in Page_Load event. When trying to update the variable holding the value of RadioButtonList.SelectedIndex in an event handler like RadioButtonList_SelectedIndexChanged it has no effect because this event will not take efect untill the page is posted back. I tried another ways but the problem in all is that any event that will detect the change in the selected index must occur after posting the page back, which I don't want to.

Is there a way to detect the selected index without posting the page back?

 
Old October 23rd, 2006, 01:14 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You'll need some client side JavaScript to this.

Take a look at the resulting HTML for the RadioButtonList. Then get a reference to the wrapping control and loop through its child controls, checking each one to see if it's a check box and then look at its checked state. Soon as you found a checked one, you can determine its index.

I am not sure if you're using the right tool for the right job. You're using a server side technology, yet you want everything done at the client. Maybe you need to take a step back, look at your design decisions again and reconsider your strategy.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing the image button on PostBack Aker C# 6 May 19th, 2008 11:20 AM
Disable a button sansircar ASP.NET 1.0 and 1.1 Professional 6 January 12th, 2007 09:38 AM
button postback does not work v_ganeshraju VS.NET 2002/2003 1 September 8th, 2004 07:33 AM
Disable Close Button Ben Horne Access VBA 1 April 12th, 2004 03:29 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.