Forum Moderators: open

Message Too Old, No Replies

Default posting of Event with Dropdown list

Load data into datagrid with default dropdown choice

         

Barbacena

7:56 am on Jul 18, 2005 (gmt 0)

10+ Year Member



Hi all!

I have an ASP.NET page (coded in C#) which contains both a datagrid (dgChoice) and a dropdown list(dlChoose). Everytime a user chooses a different option in the dropdown list, different data corresponding to this choice is loaded into the dgChoice with help from a function that looks like this:


private void dlChoose_SelectedIndexChanged(object s, System.EventArgs e){

string strSelected = dlChoose.SelectedItem.Value;
switch (strSelected){
case "0":
dgChoiceSource = "Path for datasource 1(default)";
break;
case "1":
dgChoiceSource = " Path for datasource 1";
break;
case "2":
dgChoiceSource = " Path for datasource 2";
break;
case "3":
dgChoiceSource= " Path for datasource 3";
break;
}

When no choices have been made, dgChoice should reflect exactly what dlChoose displays when no selections have been made (Path for datasource 1) which is the default choice for both grid and dropdown.

The problem is that this doesn’t happen. The grid(dgChoice) doesn’t show up until you changed the selection in the dropdownList(dlChoose).

How do I make it so that the grid always shows data and always corresponds to the choice in the dlChoose dropdown, even if no selections have been made?

If anyone could help with this, I’d be very grateful.

Thanks!

tomasz

7:24 pm on Jul 18, 2005 (gmt 0)

10+ Year Member



Remember your SelectedIndexChanged with fire only on postback. You need to populate your default settings on pageload.

On page load
1 Load your dropdown
2 Set your dropdown to SelectedIndex=0 or set to user saved setting
3 Load your grid etc..

Barbacena

1:22 pm on Jul 20, 2005 (gmt 0)

10+ Year Member



Thanks for replying

I think that I see what you mean, basically the grid data should be loaded anyway with their default content and the dropdown menu should show the default choice which corresponds to the grid’s default content.

That way the grid’s content only load again, if a different choice was made in the dropdownlist.

I’m getting this right?

I thought that there was a way to synchronise the dropdown’s contents and grid even prior making a choice, but obviously that cannot happen because of the postback.

Is there a way to make it happen if it is set to false? And will this affect the way in which the dropdown works for other choices?

regards

tomasz

2:49 pm on Jul 20, 2005 (gmt 0)

10+ Year Member



Actually, You can
Let say you have item to preload
What you do is
On PagaLoad
1. Load your dropdown
2. Get item you want to load from session variable query string, etc.
3. set selectedindex property using
SetComboBoxByValue(droppdown,combovalue)
4. Load your grid with dropdow.SelectedIndex or Value or Text

Private Sub SetComboBoxByValue(ByVal ctl As DropDownList, ByVal value As String)
Dim i As Integer
If value <> "" Then
For i = 0 To ctl.Items.Count - 1
If ctl.Items(i).Value = value Then
ctl.SelectedIndex = i
Exit Sub
End If
Next
End If
End Sub

Barbacena

1:28 pm on Jul 25, 2005 (gmt 0)

10+ Year Member



I'm using C# but I think I understand what you mean. But I'm going to leave as loading the default content rather than having to use sessionvariables. But thanks for your reply, it helps.

aajiz

3:40 pm on Jul 25, 2005 (gmt 0)

10+ Year Member



Below is one way to do it. It is not optimized and that is not how I would do it. But, it will probably work with your code.

private void Page_Load(...) {
if (!Page.IsPostBack) {
dlChoose_SelectedIndexChanged(null, null);
dgSource.DataBind();
}
}