I am setting up a page to allow users to create their own pool cues. As they select a feature they want on their cue from a dropdown list it needs to update the price onscreen but not refresh the whole page. I'm guessing I would use an update panel ? Not sure where and how to add it. The selected value passed to the VB code is the extra price to pay (Eg: £10 extra for a jointed cue). Here's what I have so far..
FRONT END
<asp:DropDownList ID="ddlJoint" runat="server" AutoPostBack="true" RenderMode="Partial" style="margin-bottom:10px" height="33px" font-size="1.6em" OnSelectedIndexChanged="ddlJoint_SelectedIndexChanged">
<asp:ListItem Value="">Cue Joint:</asp:ListItem>
<asp:ListItem Value="10">3/4 Join</asp:ListItem>
<asp:ListItem Value="0">One Piece</asp:ListItem>
</asp:DropDownList>
<br>
<asp:Label ID="lblTotal" runat="server" Text="Price:"></asp:Label>
VB
Protected Sub ddlJoint_SelectedIndexChanged(sender As Object, e As System.EventArgs)
session("joint") = ddlJoint.SelectedValue
session("total") = session("joint") + session("tip")
lblTotal.Text = session("total") + 180
End Sub
Your gut feelings are correct. You need an update panel. Here is an article how to use update panel with drop down list:
http://www.c-sharpcorner.com/uploadfile/Dorababu742/drop-down-list-in-ajax-update-panel/
Using an update panel is a great was to “avoid” having to wire up your own AJAX calls and updates.
And it gets rid of post-backs rather nice.
(So hitting the browser back button does not go to a previous post back for that given web page).
Make a blank test web form.
In web form design mode, simply:
From tool box, simply drag + Drop in the scrip manager.
(You can search for script in search box, but you find this under AJAX Extensions.
Now drop in an update panel
(Again you find this under AJAX Extensions).
Now drop in a button and text box “inside” of the update panel.
And for fun, let’s drop in another button below (outside) of the update panel.
Double click on the first button (to bring up vb code editor).
Simply type in this code:
me.TextBox1.Text = "post without post back"
Behind that button again double click, and write this code
me.TextBox1.Text = "post back"
Your form will look like this:
Note how when you click the button inside the update panel, the browser does not show or “light up” the back button.
However, if you hit the button outside of the update panel, you see the browser “spin” – a post back occur and the back button becomes active.
For you existing page, you can drop in the scrip manager near the top.
Then where your controls are drop in an update panel.
Then select the controls you want and drag + drop (or even cut + paste into the update panel area).
The beauty of above is you really don’t have to change your coding habits or learn JavaScript to “wire” update areas of a web page that update without post-backs.
The resulting vb.net code looks like this:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.TextBox1.Text = "hello"
End Sub
Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.TextBox1.Text = "hello with post"
End Sub
Related
I am using a repeater control in my page and bound records from stored procedure. It has about a thousand records. I have used the pagination as well. I have a column for link button to navigate into a another page like below.
<asp:LinkButton ID="btn1" runat="server" Text="Link1" CommandName="link"
CommandArgument="222">
In code behind
Protected Sub repeater1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs)
Select Case e.CommandName
Case "link"
Response.Redirect("/page2.aspx?" & e.CommandArgument, False)
End Select
End Sub
When clicking on the "Link" button it opens the page2 and takes about 25 seconds. There is only code in page2 load event to show the command argument in a textbox. So Why it is taking much time to load the page ? If there are only few records, its working fine. Here I am using a parent and a child repeater to bind the records in order to show the record grouping.
I tried to trace the child page (page2.aspx) load event as well the master page (overall master page) load event. All seems taking less time.
I have a busienss requirement that, it needs to open a pop and also page redirection to be done in the same Submit button click event, presently i did both, but only Page redirection is happening, the popup is not working.
On commenting the Page redirection, the popup is working. Can anyone give me the solution for this?
Eg.
Private Sub wbtnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles wbtnSubmit.Click
wbtnSubmit.Attributes.Add("OnClick", "win_openPopUp();")
ClientScript.RegisterStartupScript(Page.GetType(), "win_PopUp", "win_openPopUp();", True)
'Also i need this redirection also...'
If ViewState("Mode") <> "E" Then
AppHelper.PageRedirect("2220", Localization.LocalizedLables.MessagesModules.MyMessages, AppHelper.PageMode.Add)
Else
AppHelper.PageRedirect("2113", Localization.LocalizedLables.MessagesModules.MyMessages, AppHelper.PageMode.Add)
End If
End
In above code, any one is working... i need, after the pop close the page should be redirected to my specifed page.
This line of code
ClientScript.RegisterStartupScript(Page.GetType(), "win_PopUp", "win_openPopUp();", True)
what does is to execute javascript win_openPopUp(); when it is reloaded the same page that did the postback . That's not what you want.
This line
wbtnSubmit.Attributes.Add("OnClick", "win_openPopUp();")
should be placed in page_load event, not here where the event has already been triggered. But you don't need to do that, as you can add this attribute in the button asp tag.
< asp:Button ID="wbtnSubmit" Text="" runat="server" OnClientClick="win_openPopUp();">
If you need more information about popups you can read
http://forums.asp.net/t/1283761.aspx?Response+Redirect+URL+and+target+_blank+
I've created a simple example to show what I'm having problems with. Place a asp.net checkbox on a page and an asp.net textbox on the same page. In the code behind (using vb.net), put code that displays something in the textbox when the checkbox is checked and displays an empty textbox when the check box is not checked. Simple, right? Now toggle back and forth several times with the checkbox checked/unchecked and the textbox has content/no content. Everything works fine - right?
Now click your back button on your browser a few times. The results being displayed with each back button click is opposite - check box is checked yet nothing in the textbox. Checkbox is unchecked and there is text in the textbox. It doesn't matter where you put the vb code (page load, checkedchanged) - same results.
I'm thinking that it has something to do with the timimg of the page being saved in cache. Something like the page is being saved in the cache while the checkmark is firing the "click" event yet the code to fill the textbox has not yet been fired. Then again I could be completely off the mark and it is something real simple that I'm doing wrong.
Any ideas/thoughts/help would be greatly appreciated.
Sample Code:
ASPX:
<asp:CheckBox id="ChkBox1" runat="server" AutoPostBack="True" text="Sample" /><br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
VB:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If ChkBox1.Checked = True Then
TextBox1.Text = "Checked"
Else
TextBox1.Text = ""
End If
End Sub
This is the way it should be.
Browsers remember every time you post back to the server. Your page is posting back whenever the checkbox state changes.
When the checkbox state is checked, the textbox is indeed empty. This is when the postback occurs. Your server side code sets the text value, and you see it in the browser.
But your back will remember what you sent, that was a checked checkbox and empty text.
The reserve case happens when you uncheck.
If you refresh one of those back pages with a F5, your browser should ask to send back post data. And you will get back to the expected state.
Tell the browser not to cache the content(if you want the browser to forget it):
Not VB code, but I am sure you will find similar functions on VB.net too.
context.Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.Cache.SetNoStore();
I am trying to set a default button in my ASPX page. I have a master page so the form is there. I have a panel in the content page that holds a table that organizes a number of textboxes, dropdowns and other inputs. The last row of the table holds some buttons, one of which I want to be the default button. After doing some research, I have tried the following with no success.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
pnlHolder.DefaultButton = cmdSearchJob.ClientID
I have also tried
pnlHolder.DefaultButton = cmdSearchJob.UniqueID
and
Dim cmdDef As Button = pnlHolder.FindControl("cmdSearchJob")
pnlHolder.DefaultButton = cmdDef.UniqueID
but both throw the exception "The DefaultButton of 'pnlHolder' must be the ID of a control of type IButtonControl.".
I have seen some Javascript solutions, but was hoping to just be able to set the defaultButton for the panel.
Try setting the DefaultButton of the parent Form.
C#:
this.Page.Form.DefaultButton = cmdSearchJob.UniqueID;
VB?:
me.Page.Form.DefaultButton = cmdSearchJob.UniqueID
Similar issue here: Allow Enter key to login in asp.net?
Try setting it to:
cmdSearchJob.ID
The panel will call FindControl to get the Client ID itself
Can you set this inside the control on the front-end?
<asp:Panel id="pnlHolder" DefaultButton="cmdSearchJob">
<asp:Button id="cmdSearchJob" runat="server" Text="Search" />
</asp:Panel>
Also, this may worth knowing, what type of object is cmdSearchJob? Is it a standard asp.net button control?
Finally found what it was. Tried adding another button with no CSS, etc. that just popped up a javacsript alert() for testing. Was able to set that as the default button when it was outside the table. As the submit button is one of a series of buttons (not a very pretty UI, but user requirements and all that) in the table, I used this additional button and set its style to display:none and have it call the same subroutine in the code behind.
So, short answer, it wasn't seeing it in the table.
Thanks everyone for your input.
I still have no idea why it wouldn't set the button.
I have written a user control that captures some user input and has a Save button to save it to the DB. I use a repeater to render a number of these controls on the page - imagine a list of multiple choice questions with a Save button by each question.
I am loading the user control inside the repeater's ItemDataBound event like this (code simplified):
Protected Sub rptAssignments_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptAssignments.ItemDataBound
Dim CurrentAssignment As Assignment = DirectCast(e.Item.DataItem, Assignment)
Dim ctl As UA = CType(LoadControl("~\Controls\UA.ascx"), UA)
ctl.AssignmentID = CurrentAssignment.AssignmentID
ctl.Assignment = CurrentAssignment.AssignmentName
ctl.EnableViewState = True
e.Item.Controls.Add(ctl)
End Sub
FYI, I need to load the control at runtime rather than specify it in the ItemTemplate because a different control could be used for each row.
In the user control, there is a linkbutton like this:
<asp:LinkButton ID="lbnUpdate" runat="server" Text="Update" OnClick="lbnUpdate_Click" />
... and a button click handler like this:
Protected Sub lbnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lbnUpdate.Click
' my code to update the DB
End Sub
The problem is that when the Save button is clicked, the page posts back, but lbnUpdate_Click is not called. The Page_Load event of the page itself is called however.
I should mention that the repeater is part of a user control, and that user control is loaded inside another user control (this is a DotNetNuke site which makes heavy use of user controls). The Save button link looks like this:
javascript:__doPostBack('dnn$ctr498$AssignmentsList$rptAssignments$ctl04$ctl00$lbnUpdate','')
This problem exemplifies how webforms outsmarts itself.
You have to reconstitute the Repeater, either by re-binding or from viewstate, to have sub-controls raise events. The price you pay is either another trip to your data source or all that redundant data stored on the client in the viewstate. Shameful!
I had a similar problem once that might be the same thing.
In short, since you are dynamically creating the buttons, after the postback they don't exist. Thus, when ASP.NET Webforms looks for the event, it can't find anything.
When does your repeater get databound? Try rendering the buttons to the page again in the postback (even as a test) to see if that does the trick.
Are the UserControl's IDs same on every postback?