I've found this question asked countless times, but the answers haven't worked for me:
I have an asp:Dropdownlist that is dynamically bound from an asp:Objectdatasource. A button calls a codebehind function to store the selected value. However, in the click event function the value of the dropdown is always reset to default, AFAIK due to a postback that is called before the click event handler. When debugging I've checked that ViewStateMode is enabled and EnableViewState is true. I've been stuck with this for hours now, does anyone have a clue?
ASPX markup:
<asp:DropDownList runat="server" DataSourceID="AvailableNivamalerODS" ID="AddNivamalerDDL" />
<asp:ObjectDataSource runat="server" ID="AvailableNivamalerODS" TypeName="Nivamaler.NivamalerPresenter"
SelectMethod="GetAvailableNivamalers"></asp:ObjectDataSource>
<asp:Button runat="server"
Text="Legg til"
OnClick="AddNivamalerToTjstpl"
ID="AddNivamalerBtn"
UseSubmitBehavior="False"
CssClass="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"/>
Codebehind
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
AddNivamalerDDL.DataBind()
End If
End Sub
Click event handler:
Protected Sub AddNivamalerToTjstpl(sender As Object, e As EventArgs) Handles AddNivamalerBtn.Click
Dim nivamalerId As Integer = AddNivamalerDDL.SelectedValue
'Here nivamalerId is always the default value
End Sub
Cheers!
EDIT
The replies to previous question have basically said to put the data binding in the Page_Init method or the Page_Load method after !IsPostBack, which didn't help me.
Also a disclaimer: This is a legacy project with tons more code (the relevant code is new), but I tried to snip out the relevant bits. As far as I can see the rest of the code shouldn't affect this, but I can't be certain as I am still fairly new to ASP.Net
Put your page_load code into the page_Init section. The asp lifecycle will make the dropdown list databind() be absolutely meaningless if it is in the page load section here because a Postback causes the whole page to resubmit itself to the point of page_load and since you have the if not ispostback statement, it will reload the page structure but won't run your page load code and that is where you are losing your value. Other than that the code is fine.
I solved it, and as has been pointed out, the posted code is incomplete: The dropdown is inside a JQuery-ui dialog, which makes the dropdown lose its state. I ended up with a workaround with a Javascript function which copied the selected value to a hidden field outside the dialog and using the hidden field value in the codebehind
Related
I've searched the forums and have yet to find a soln to my specific problem. I'm writing an asp.net aspx page with
<asp:Button ID="Add" OnClick="Add_Click" runat="server" Text="Add" CausesValidation="False"/>
where Visual Studio even autofilled my OnClick function, and my VB code behind function looks like this:
Protected Sub Add_Click(sender As Object, e As EventArgs) Handles Add.Click
Server.Transfer("TimeReportingAdd.aspx", False)
End Sub
I have another page with a button set similarly and it works fine. Does not even hit event during a debug. The main page (TimeReporting.aspx) just refreshes on the click and I would like it to redirect to another page (TimeReportingAdd.aspx). The button was not copied but created new and the event was created by double clicking in the designer. runat="server" is present in all my content headers etc. I don't think my TimeReporting.aspx page header needs any validation elements because the page that works doesn't have them. The Add button is declared as follows in my vb code behind:
Protected WithEvents Add As Global.System.Web.UI.WebControls.Button
Also, the button that works in another page is easier, doesnt even have onclick or validation:
<asp:Button ID="Button1" runat="server" Text="Query" />
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
...
End Sub
I've tried deleting the button and adding it again multiple times. Thanks in advance!
Please remove OnClick="Add_Click". This will fix your problem.
There are two things you need to consider:-
When you add OnClick, this means you want to handle click in Java Script.
To handle click in server side, you provide a method in code behind with Handles clause.
First approach overrides second one.
I was able to get this to work by giving this property in my button:
PostBackUrl="~/TimeReportingAdd.aspx"
It was posting back to the initial page TimeReporting.aspx
Having an issue with an ASP.NET GridView is empty on postback that I need some help with. I think it may have something to do with the ViewState not being setup. Anyhow I originally had the code working on single user-form until I refactored code.
Now to paint the picture I have now both a master page and a base form. My master page has the place holder and on my actual user-form I have placed the GridView within the place holder bounds as follows:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderMainBody" Runat="Server">
<asp:GridView ID="data" runat="server" AutoGenerateColumns="false" EnableViewState="true" ...>
...
</asp:GridView>
</asp:Content>
One of fields in the GridView is an editable comments field mutli-line textbox (the rest are non editable):
<asp:TemplateField HeaderText="Comments">
<ItemTemplate>
<asp:TextBox ID="TextBoxComments" runat="server" TextMode="MultiLine" Rows="4" Columns="40" Text='<%# Bind("Comment")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBoxCommentsEdit" runat="server" TextMode="MultiLine" Rows="4" Columns="40" Text='<%# Bind("Comment")%>' />
</EditItemTemplate>
</asp:TemplateField>
I edit one of the rows and click a submit button to postback. The GridView has 10 rows to enter into however on postback there are zero rows so my saving is lost!
My base form contains the code in the OnInit event to load the submit button and thus also handles the click event.
My OnLoad event I call the base Onload which inturn calls my user form's Page_Load handler code which has one line of code namely:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
MyBase.data = Me.data
End Sub
and in the BaseForm is declared as:
Protected WithEvents data As GridView
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
If Not Page.IsPostBack Then
...
BindData(...)
...
End If
End Sub
in this way I can also handle all GridView events in the BaseForm.
So somewhere between the master/baseform/userform/viewstate relationship my GridView data is lost on PostBack. Any ideas?
On your Page_Load, bind the data only if IsPostBack is false.
You click on submit button that submit button fire RowUpdating event and that event contain query for update database table and after executed update query call BindData() function in your code .
Three in row I think for myself answering my own question - hooray! I do not know if that makes me intelligent or dumb because I have to search for more that a day to find a solution. Perhaps I did not give out enough information or it was not clear and this is what happens when you do things for the first time and you do not have a clue what you are doing. The vital information which was maybe not implied but hinted at, which I will spell it out for anyone else that might have the same problem, is I left out mentioning in my OnInit method I call the following code:
Dim cpl As ContentPlaceHolder = Master.FindControl("ContentPlaceHolderFooter")
btnUpdate = New Button
btn.ID = "btnUpdate"
cpl.Controls.Add(btnUpdate)
I know the purest will say why did you not add the button to the footer of the grid as opposed to an additional content placeholder in the master page - well with egg on my face I didn't.
Anyhow I moved the code above to the CreateChildControls overridable method and I also required an additional call to EnsureChildControls in my OnLoad event so my OnInit method with emphasis disintegrated!##%^* Why? Well the answer was hinted at within the answer to the other question asked on this site I mentioned in my second comment to "Rajan Chauhan" that I checked out and that is apparently whenever you iterate through the collection of controls you mess with the ViewState (hey I am just re-iterating what was said in the other post I have no authority on the matter) before it gets loaded so calling Master.FindControl is a no-no inside OnInit!
However, saying all that my RowUpdated event does not fire as I am actually editing in view mode because of my ItemTemplate markup so I will stick with what I have as my btnUpdate_Click event still works as before i.e. it does some magical code that I found on some other site that checks each row one by one for change of data and then updates that particular row only. Well I can as there is only 10 rows at most so I do not overload the ViewState too much and if it is important to know I also use paging so in reality I have more than 10 rows but did not want to mention that as I thought that might add to the confusion.
I'm looking for a solution for the textbox leave event in asp.net vb.net.
I have searched but Didn't get the right solution, what I'm actually doing is.
I have textbox in which a user write the Product Id number however the focus moved from the textbox then all the related data should be displayed on the concern textboxes.
Their is no textbox_lostfocus event or textbox_keydown event available in ASP.Net. You can do the same by writing the code in TextChanged event of the Text Box.
Your code will be like the following :
Private Sub txtamount_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtamount.TextChanged
//Your code comes here
MsgBox(txtamount.Text)// sample display
End Sub
This code will give result only when you add AutoPostBack ="true" with your Textbox design.
ie., the ASP code for the textbox will be :
<asp:TextBox ID="txtamount" runat="server" AutoPostBack ="true" />
Hope that this is actually your are asking for.
I have a problem on one page in my entire solution where it seems the on click event for the image button is not firing.Firstly let me describe the relevant parts of the website.
There is a total of four pages involved in this problem.Pages A-D.
I start on Page A from which I navigate to Page B via an ImageButton which works.Here some data is captured and I navigate further to Page C via another ImageButton which works.On Page C I have two ImageButtons,these are the ones giving me trouble.One is simply supposed to take me back to Page A and the other takes me to Page D.The one that takes me to Page D I send some arguments along.
Now if I click on either of these I get taken back to Page A,however when debugging even on the one thats supposed to take me back I cant see the code being hit.Here is how I have my buttons defined in the aspx page:
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/backBtn.jpg" />
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/nextBtn.png" />
And my code behind for them is as follows:
Protected Sub ImageButton1_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
Response.Redirect("pageA.aspx")
End Sub
Protected Sub ImageButton2_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles ImageButton2.Click
Response.Redirect("pageD.aspx?option=4&ref=" & lblInfo.Text)
End Sub
The buttons aren't in a update panel.Things I have tried to do to fix this :
Remove them and readd them from scratch.
Turn into normal asp:Button to see if its a problem with ImageButtons,problem was still there.
Set AutoEventWireUp to true.
Explicitly set OnClick in aspx to the method.
Set CausesValidation to false.
None of the above has worked.If anyone can help I would be very gratefull.
No url rewriting or default document involved ?
Could it be some code in pageC or some base class, which triggers on postback before click-events handling, and redirects to page A ?
(maybe some error handling which redirects to pageA)
I'm trying to build a form which generates itself as it is used. I have created a really simplistic example, loosely related to what I'm trying to do below, but which demonstrates the problem.
The user types a word in the text box, clicks the Button and a new TextBox is loaded into a Panel, with the value in the original TextBox that the user has entered. The user should then be able to type something else/the same and create another text box with that in it by clicking the button, basically permitting 0,1,..,n textboxes appearing above the "txtFeeder" TextBox on the form.
The problem is that everytime you click the button, it doesn't add a new control, it seems to just update the one that has already been created with the new (incremental) ID. I'm not sure if I'm doing something wrong, or if what I'm trying to do can't be done (which I find hard to believe)?
Here's the .aspx...
<form id="frmMain" runat="server">
<asp:Panel ID="pnlAdded" runat="server"></asp:Panel>
<asp:TextBox ID="txtFeeder" runat="server"></asp:TextBox>
<asp:Button ID="btnFeedPanel" runat="server" Text="Button" />
</form>
...and here's the .aspx.vb...
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
ViewState.Add("elementCount", 0)
End If
End Sub
Protected Sub btnFeedPanel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFeedPanel.Click
ViewState("elementCount") += 1
Dim txtNew = New TextBox
txtNew.ID = "txtElement" & ViewState("elementCount")
txtNew.Text = txtFeeder.Text
pnlAdded.Controls.Add(txtNew)
txtNew = Nothing
End Sub
Thanks
On PostBack you need to explicitly regenerate the buttons from ViewState (you check the added counter you have in the viewstate and regenerate the added buttons) - otherwise they'll be gone (and only the original one will appear, as you're experiencing).
Have a look at this question, the guy is trying to achieve smt extremely similar to what you're looking for (maintaining a bunch of dynamic buttons and regenerating them on postback).
Controls that are added to a page dynamically are not automatically retained between form posts. The control itself isn't preserved in the page's view state. I think you'll need to rebuild all of the previously added fields every time a postback occurs.