im working on asp.net with c#, and I have a gridview with textboxes and dropdownlists.
I have data loaded from an sql database into the gridview with the GridView1_RowDataBound.
I have a linkbutton which once is clicked brings the GridView1_RowEditing event, this enables the whole row for editing. But when this happens, all the data for the corresponding row is gone. All the textboxes and dropdownlists have no value.
What can I do, so the values stay on the controls once the RowEditing event is fired?
Be sure that you are not data binding after the control has been loaded. Be sure that you are populating the data for your control only after making sure the page is not being posted.
If Not Page.IsPostBack Then
'Populate control
End If
Otherwise the control data will be reset as viewstate data is loaded on PreInit before the Load event is called.
Also, ensure you have viewstate enabled unless you are repopulating the control data.
Related
I have a dropdownlist control and a text box control in asp.net. dropdownlist is getting data from database. suppose some data are x-ray,USG,MRI and their value are 100,200,300. So if i select x-ray from the dropdown i want it's value 100 binds at the textbox without page refresh. How can i do this??? Please Help.
Using .net, to have something happen on the server side when something is selected from DropDownList, having property AutoPostBack=true is required. This means when you change the SelectedIndex, the page refreshes (postback), then the SelectedIndexChanged event is fired.
Theres a few approaches. In your page_load event, put everything you want to happen on the first page load in If Not IsPostBack Then ... End If block. Then when a PostBack happens, like DropDownList_SelectedIndexChanged, the code in If Not IsPostBack Then won't happen, because it IS a postback.
The above approach still has a page reload, but you can choose what happens when its this type of PostBack page reload. You may have javascript thats re-firing, though...
The second approach would be to have no postback event happen from the dropdownlist, and handle the Binding of the selected item in DropDownList to the TextBox with javascript.
I have a button on my page and and few textboxes which alters their context on page_load event. when I open the page for the first time it loads random data from a database to textboxes with a function in pageload and also I create those textboxes dynamically. what I want is to make some changes on textboxes texts and click the button than send new context of textboxes to database. But when I click on button, PageLoad event fires again and It regenerates the textboxes and the context of them alter naturally since they are newly created textboxes. I tried to use "if(!Page.isPostBack)" in pageload but then the textboxes and related objects are not being created and they become null.
how can I overcome this problem?
best thing I can think of is to find some way to fire buttons click event without firing PageLoad event, but I dont know how can I do it.
any help would be appreciated;)
From Comments
Can you regenerate the Textbox on each Page load Execution? and data assignment to these Textbox should be under !Page.IsPostback. All you have to do is to keep the IDs of the Textbox in ViewState only first time page load and next regeneration of Textbox will have the previous stored ID and you also have to store the ID corresponding data in ViewState. Makes sense?
So on the .NET GridView control there's a RowDataBound event, at which time I create a DropDownList for each row in a particular column.
When that DropDownList changes on the client, I can get it to post back, but the control's state is already gone. What's the best approach to handling events for controls created during the RowDataBound event?
I'm curious: why are you adding these DropDownLists to the GridView in the RowDataBound event? I genuinely can't think of a reason to do this.
Even if you have just a placeholder DropDownList in the original RowTemplate, you should be able to bind the control and assign appropriate event handlers.
On my asp.net page, I have several DropDownLists.
I also have a Repeater.
In the ItemDataBound event I want to get the value of these DropDownLists, to change the data in the Repeater. The SelectedValue of these DropDownLists is empty.
But after the ItemDataBound, the Page_Load is executed. There I can get the value of these DropDownLists.
Is there a solution to get the value when the ItemDataBound is executed.
thanks!
Filip
You need to data-bind these drop-down lists in the Page.Load event.
There're a lot of web controls that get their state or other details during load life-cycle (I had these kind of problems long time ago).
NOTE: When I say "State" I'm not talking about ViewState.
And why don't you do that data-bind after the load event?
Can you get the drop down lists' selected values in the page PreInit event? If so, store them in view state and retrieve them from view state during the repeater's item data bound event.
If that doesn't work, try adding a selected index changed event to each drop down. When the drop downs change, set a view state variable that you can retrieve during the repeater's item data bound event. If you have values to which you are setting the drop downs during page load, such as when reading from a database, use those values to directly set the appropriate view state variables.
Assume I have an asp:GridView with some customer information. On that page I also have a button which allows me to add a new user to the GridView. When I add the click the user, I databind the gridview again in the button click. Correct me if I am wrong, but if the asp.net lifecycle or at least part of it is in the following order:
page_load
Hookup event handlers such as my button click
page_preRender
Does this mean that if I put the databinding for the gridview in preRender, that is the only place I need to worry about calling it. I can remove it from the button click. This is what I think, but I am not sure if my thinking is correct, so I would like some more insight as to the benefit of putting code in PreRender as opposed to PageLoad
By PreRender, you assume that most logic in the page that would affect the binding result has been completed (usually in Page_Load, but anywhere earlier really).
Controls in the .Net framework by default do their binding in the PreRender event (makes sense, bind to the datasource at the last possible moment...could be that you changed the data 400 times earlier in the lifecycle). Here's the full layout of the 2.0 lifecycle for reference.
PreRender - Before this event occurs:
The Page object calls EnsureChildControls for each control and for the page.
Each data bound control whose DataSourceID property is set calls its DataBind method. For more information, see Data Binding Events for Data-Bound Controls later in this topic.
The PreRender event occurs for each control on the page. Use the event to make final changes to the contents of the page or its controls.