databound dropdownlist select an item - asp.net

I have a databound dropdown list (ASP.net). I want the page to load with a certain item as the selected item.
I am not adding a blank first row (thats not what i need)
I find that I can get this to work with "AppendDataBoundItems" to true, but the side-effect is that I have all the items listed twice.
thanks for your help!

Use the Page_PreRender event to handle this situation....
In the Page_Load register an event handler for the PreRender event
protected void Page_Load(object sender, EventArgs e)
{
Page.PreRender += new EventHandler(Page_PreRender);
}
And in the PreRender event,
void Page_PreRender(object sender, EventArgs e)
{
ComboBoxSomething.SelectedValue = WhatEverYouWant;
}

Related

How do that gridview table will not redownload when user push update button?

When user push update button DataBound event starting. How to lock this event? It's take much time to wait when gridview enumerates every row.
You should not DataBind your GridView on postbacks. So just wrap it in a !IsPostBack:
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
DataBindGridView();
}
}

How do i postback?

I have this web application which is supposed to move rows up and down on button click, but it does only when I close the webpage and re run my program from Visual Studio.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
DataView view = (DataView)SqlDataSource1.Select(
DataSourceSelectArguments.Empty);
DataTable result = view.ToTable();
}
}
Is this all I need ?
You can use the CommandName to trigger the functionality of your button clicks.
Example: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.commandargument.aspx
You need to use a button click event. You logic is currently in the page load event, that's why its only firing once
You could also remove the if postback which would also work but is not the recommended solution
protected void Page_Load(object sender, EventArgs e)
{
DataView view = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
DataTable result = view.ToTable();
}

Page_Init and Page_Load

A page containts custom address control and checkBox. Why does the second example of code work properly, but first doesn't?
//1
protected void Page_Init(object sender, EventArgs e)
{
//doesn't work properly
ucLegalAddress.Visible = !chkLegalAddress.Checked;
}
//2
protected void Page_Load(object sender, EventArgs e)
{
//works properly
ucLegalAddress.Visible = !chkLegalAddress.Checked;
}
Because the viewstate of the controls is loaded between the init and the load event. This means that the init event does not know the state of the client yet.
MSDN lifecycle overview
Because all controls are create in OnInit() method, that call between Page_Init and Page_Load. In Page_Init all controls are null. Read more

Handle Button Click Event from User Control loaded dynamically

I have a blank user control (child) and during Page_Load i create some text boxes and a button. I also add an event to the button.
I then load that user control dynamically from a placeholder in another usercontrol (parent).
Both controls are rendered correctly but when i click on the button, the event isnt fired.
Any ideas on how to handle the event?
Child code:
protected void Page_Load(object sender, EventArgs e)
{
/*... other user controls ..*/
UpdateButton = new LinkButton();
UpdateButton.ID = "buttonid";
UpdateButton.Text = "text";
UpdateButton.Click += new EventHandler(UpdateButton_Click);
this.Controls.Add(UpdateButton);
}
protected override void Render(HtmlTextWriter writer)
{
for (int i = 0; i < this.Controls.Count; i++)
{
this.Controls[i].RenderControl(writer);
}
}
public void UpdateButton_Click(object sender, EventArgs e)
{
//Do stuff
}
Parent code:
protected void Page_Load(object sender, EventArgs e)
{
placeholder.Controls.Clear();
ChildControl uc = (ChildControl)LoadControl("~/UserControls/ChildControl.ascx");
placeholder.Controls.Add(uc);
}
If i use the child control directy (ie without the parent the control) the click event is raised and handled nicely. But when i use the parent control, the debugger wont even hit a breakpoint inside UpdateButton_Click().
Thanks in advance.
I think I know what might be happening. In your parent Page_Load you are calling placeholder.Controls.Clear(); This does what you would imagine and clears the control, including any events that have occurred. What happens when remove this line? Do you get an additional one created on each postback?

FormView not updating with control events

Time for my daily ASP.NET question.
One of my pages shows all of our customer information from a customer table. I want the user to choose whether to see all customer records, or select a specific record from a list. So, my webpage has two radio buttons (show all customers, show specific customer), a listbox (full of customer names), and a formview control. The problem I'm having is getting the formview to update when I change modes via radio buttons or listbox selection (see code below).
Can anyone provide me with some pointers on how to do what I'm trying to do?
protected void Page_Load(object sender, EventArgs e)
{
UpdatePage ();
}
protected void RadioButtonShowAll_CheckedChanged(object sender, EventArgs e)
{
}
protected void RadioButtonShowSelected_CheckedChanged(object sender, EventArgs e)
{
}
protected void DropDownListCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
RadioButtonShowSelected.Checked = true;
UpdatePage ();
}
protected void UpdatePage ()
{
if (RadioButtonShowAll.Checked)
SqlDataSource1.SelectCommand = "SELECT * FROM [Customer] ORDER BY [Company]";
else
SqlDataSource1.SelectCommand = "SELECT * FROM [Customer] WHERE ([CustomerID] = #CustomerID) ORDER BY [Company]";
FormView1.DataBind();
}
First, you only have the SelectedIndexChanged event wired up... In that case, what happens when you change the drop down box is first, Page_Load() fires--which calls UpdatePage(). Then, the event fires, which calls UpdatePage() again. The second time probably doesn't do what you expect.
The fix is to only call UpdatePage() from Page_Load() the first time the page is loaded, but not from postbacks:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
UpdatePage();
}
Your page has to be updated on Client side, for it to show the new data. You'll need to use Javascript or AJAX and have some variable that keeps track of the need to refresh your page, this way you can send a request to update the page to the server when the Formview needs updating.

Resources