this is my application link please download
i had 2 different user controls in one .aspx page.
first user control was dropdownlist binded with database.
second user control was gridview.
if i select a company int the dropdownlist products of that company should be displayed in the second user control.
my problem was, when i am selecting the companies in the dropdownlist gridview user control is loading first and 2nd dropdownlist selected event is firing..
please help me...
You should probably check the IsPostBack property and only populate the controls if it isn't a postback.
void Page_Load()
{
if (!IsPostBack)
{
// Populate the user controls.
}
}
But since you didn't show any code I'm just guessing.
Related
I have a GridView and a Button in a web form.
I change SelectCommand of SqlDataSource when user click on the button.
Then the result has been shown in GridView. But when the page is refreshed, results changes. For example when user wants to change the page of GridView.
Why?
Seems that your data is bounded again to grid for every Page load.
Please make sure that the the setting the datasource and databinding is not done on every page load..
Put your code in (!IsPostBack) like this:
if(!IsPostBack)
{
Gridview1.DataSource=yourDataSource;
GridView1.DataBind();
}
I am using a FormView to select companies from a database. This FormView is bound to an ObjectDataSource, which is bound to a DropDownList. When a user selects a company from the DropDownList, the FormView is populated with information pertaining to the company (AutoPostBack is enabled on the DropDownList to provide dynamic record changing). My FormView supports insert, update, and delete functions, of which all three work.
My problem is as follows: After I edit a record in the FormView, the record successfully changes, however, it is completely removed from the screen. Only after I select a new company from my DropDownList will a record re-display in the FormView.
Does anyone have an idea as to what may be the issue here?
You need to databind again.
void FormView_ItemInserted(Object sender, FormViewInsertedEventArgs e)
{
Gridview.Databind();
}
You need to trigger the child control to be databound when the parent control has been modified.
I've found that the most reliable palce to do this is in the parent control's DataBound event. After a successful edit, this event is called, because the control needs to re-bind to the edited data.
Here's a snippet from some actual code of mine where I do something similar. In it, I have a control called QuestionGroupResutls that has child records in a control called QuestionResults.
void QuestionGroupResults_DataBound(object sender, EventArgs e)
{
QuestionGroupsEditingDetailsView1.DataBind();
QuestionResults.DataBind();
}
I have this in Page_Load, but you can specify this declaratively in the asmx (I just don't have a code sample).
QuestionGroupResults.DataBound += new EventHandler(QuestionGroupResults_DataBound);
I don't know the name of your controls, but you should be able to do the same thing with yours.
I have the following situation:
Master page contains a tree control
Page use the previous master page
Control populates its items based on the selected node in the tree (which is in the master page)
The question is:
Where should I populate a control based on a selected value from master page?
The problem is that the Load event of the master page is fired after the Load event of the page, so I couldn't handle the selected node from the master page in the Load event of the page.
I tried also to use the PreRender event of the page, but the control is not populated true.
Any help!
Based on your question I'm guessing you are building/binding the list in the masterpage's load event and then reading the selected index/value in the child pages load event.
Instead try building/binding in the masterpage init event, then reading it in the pages load event should be no problem.
Set the NavigateUrl to the page which should be loaded and pass the selected node value as query strings.
In the example below, I'd subscribed to OnTreeNodeDataBound event on the TreeView.
In the event handler, I'd customized the NavigateUrl by adding query string (in this case selected node value).
ex:
TreeView Markup in Master page:
<asp:TreeView ID='TreeView1' runat='server' DataSourceID='SiteMapDataSource1' OnTreeNodeDataBound='HandleOnTreeViewTreeNodeDataBound'>
</asp:TreeView>
code-behind:
public void HandleOnTreeViewTreeNodeDataBound(Object sender, TreeNodeEventArgs e)
{
String newUrl = String.Format("{0}?nodeValue={1}", e.Node.NavigateUrl, e.Node.Value);
e.Node.NavigateUrl = newUrl;
}
I have a Listbox getting populated when the page loads itself and also in postbacks. Some items are already selected in that listbox even when the page loads or postbacks. I have to select one or more items in that textbox in addition to the selected one.
Till this point, it works perfectly, but the problem is that when i click an "update" button to save the newly added items, then also the page postbacks and so the list box get populated again. This will loose the newly selected items and none of the newly selected items will be saved.
So what should i do in order not to loose the newly selected items when i click the "update" button.
Note:- I need to populate the listbox on postbacks also. So the population of that listbox on postbacks cannot be discarded.
Kindly help me. I am new to ASP.net.
Thanks in Advance
You shouldn't need to re-populate the list if you have your view-state enabled.
To populate you should be doing something similar to this:
if(!IsPostBack){
// populate your list from database or whatever
}
This will only populate the list if the page is not a post-back, therefore (if viewstate is enabled) your selections will remain after you click update.
If you want to disable the viewstate, it is important to populate the DropDownList during the "Init" event of the Page LifeCycle not the "Load" event:
protected void Page_Init( object sender, EventArgs e ) {
DropDownList1.DataSource = ...;
DropDownList1.DataBind();
}
More information can you find in this excellent article:
Truly understanding viewstate.
I hope this helps a bit.
I've a following requirement for my asp.net page:
User can add a textbox dynamically on a page A by clicking on link "Add a new category" hyperlink
He clicks submit button on page A and gets redirected to page B.
When he clicks on page A link from this page, the textboxes that he added should be persisted.
Can someone help me with the code on this?
Appreciate your help!
In the ButtonClick Method write.
TextBox tb = new TextBox();
Parent.Controls.Add( tb );
The Parent is the control you want to add the textbox to, for instance a panel.
You can also look at this resource.
Hope it helps.
Adding a user control dynamically is simple. But in this case, I don't think you need to do that, instead you should look at creating a repeater with a textbox inside it, and when the user clicks Add Category, add one item to the repeater datasource.
This way you can handle both control creation and state persistence at the same time.
dealing with dynamic user controls can be a pain in the ass.
as a rule of thumb i follow, whenever you create a dynamic user control, then you must set it's ID so ASP.net can reallocate it on post back, and to keep the controls values after post back you should reload your user controls on Page_Init event.
hope this helps.
Dynamically creating Textboxes:
suppose you have page like this
when you enter '1' in textbox and click on ADD button,output will be like display one textbox
below..
i have designed like this,
i have one textbox and placeholder for displaying dynamic textboxes..
double click on Add button...in btnadd_click,you have to write the following code
protected void btnadd_Click(object sender, EventArgs e)
{
int i;
for (i = 0; i < Convert.ToInt32(txtno.Text); i++)
{
TextBox txtbox = new TextBox();
phtxt.Controls.Add(txtbox);
phtxt.Controls.Add(new LiteralControl("<br>"));
}
}
debug it... and the output is,
As others have stated adding the text box dynamically is fairly straight forward, just create the Textbox and add it to the controls collections wherever you need it to show up. You then need to store the information that this user gets this additional text box. Assuming that this is meant for long term, you will need to store this information in your backend store. Whenever you are constructing the page you will need to read the store information first to see what textboxes to create.
I would suggest doing it as follows. In the Onload event, if you have not done so before, load the dynamic information from your DB. Add any necessary controls to the page and store this information in viewstate. On any subsequent postbacks, read the information from viewstate to add the additional controls. This will save you from having to read constantly from the database on each postback.