Handling Form Field Changes - Events?

  • Thread starter Thread starter Matthew Workman
  • Start date Start date
M

Matthew Workman

I have one aspx page where i am databinding two drop down lists based on a dataset (the data is being loaded from an XML Document). The fields get updated properly, no problem there. My question is: "How do you handle the events so that if the user chooses an option in the drop down, it poulates the cooresponding value in the second drop down?"

I have been splitting hairs on this, and I am at a point that I need some help from the community. Here is a look at the code I am using:

testpage.aspx:
<asp:DropDownList id="client" runat="server">
<asp:DropDownList id="wsname" runat="server">
testpage.aspx.cs
private void populate_fields()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("params.xml"));

this.client.DataSource = ds.Tables[0];
this.client.DataTextField =ds.Tables[0].Columns[0].ToString();
this.client.DataBind();

this.wsname.DataSource = ds.Tables[0];
this.wsname.DataTextField =ds.Tables[0].Columns[1].ToString();
this.wsname.DataBind();

}
params.xml
<?xml version="1.0" encoding="UTF-8"?>
<clientList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<client>
<name>Client1</name>
<webservicename>WebServiceName1</webservicename>
</client>
<client>
<name>Client2</name>
<webservicename>WebServiceName2</webservicename>
</client>
</clientList>
Any help would be highly cherished.

Regards

Matthew Workman
 
If the dropdowns are set to AutoPostBack, when the 'SelectedIndexChanged' server-side event fires and your page PostBack's, you can set the second dropdown's selectedIndex property to the selectedIndex property of the first dropdown. It appears the index's should match since they are loaded from the same row in the datasource table.

Another way that may be quicker for the user would be to use Javascript client-side to do the same thing since you don't have to get any more data from the server.

-Darrin

I have one aspx page where i am databinding two drop down lists based on a dataset (the data is being loaded from an XML Document). The fields get updated properly, no problem there. My question is: "How do you handle the events so that if the user chooses an option in the drop down, it poulates the cooresponding value in the second drop down?"

I have been splitting hairs on this, and I am at a point that I need some help from the community. Here is a look at the code I am using:

testpage.aspx:
<asp:DropDownList id="client" runat="server">
<asp:DropDownList id="wsname" runat="server">
testpage.aspx.cs
private void populate_fields()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("params.xml"));

this.client.DataSource = ds.Tables[0];
this.client.DataTextField =ds.Tables[0].Columns[0].ToString();
this.client.DataBind();

this.wsname.DataSource = ds.Tables[0];
this.wsname.DataTextField =ds.Tables[0].Columns[1].ToString();
this.wsname.DataBind();

}
params.xml
<?xml version="1.0" encoding="UTF-8"?>
<clientList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<client>
<name>Client1</name>
<webservicename>WebServiceName1</webservicename>
</client>
<client>
<name>Client2</name>
<webservicename>WebServiceName2</webservicename>
</client>
</clientList>
Any help would be highly cherished.

Regards

Matthew Workman
 
That is helping a lot...

One quick question on this, what would you recommend for controlling what code is executed on that actual post back then? Right now when the post back occurs is goes through the Page_Load and looks at whether or not it was post (Page.IsPostBack) then executes all the code on the page. If I want to update just a single field, I want to try to avoid all the rest of the code. Any thoughts?

Thanks a bunch.

Matthew.

If the dropdowns are set to AutoPostBack, when the 'SelectedIndexChanged' server-side event fires and your page PostBack's, you can set the second dropdown's selectedIndex property to the selectedIndex property of the first dropdown. It appears the index's should match since they are loaded from the same row in the datasource table.

Another way that may be quicker for the user would be to use Javascript client-side to do the same thing since you don't have to get any more data from the server.

-Darrin

I have one aspx page where i am databinding two drop down lists based on a dataset (the data is being loaded from an XML Document). The fields get updated properly, no problem there. My question is: "How do you handle the events so that if the user chooses an option in the drop down, it poulates the cooresponding value in the second drop down?"

I have been splitting hairs on this, and I am at a point that I need some help from the community. Here is a look at the code I am using:

**Snip**
 
Left click on the first dropdown box, and click 'Properties' from the context menu. At the top of the Properties window, click the lightning bolt, which will list server side events. Find the one that says 'SelectedIndexChanged' and double click it. It will create the event with the proper paramaters and will execute server side whenever the combo box index is changed. Make sure the 'AutoPostBack' property of the combobox is set to 'True'.

Also, make sure that anything that you don't want to happen everytime the page postback's is in an if structure:

if(!IsPostBack)
{
//Load combo boxes the first time only here.
}

-Darrin
That is helping a lot...

One quick question on this, what would you recommend for controlling what code is executed on that actual post back then? Right now when the post back occurs is goes through the Page_Load and looks at whether or not it was post (Page.IsPostBack) then executes all the code on the page. If I want to update just a single field, I want to try to avoid all the rest of the code. Any thoughts?

Thanks a bunch.

Matthew.

If the dropdowns are set to AutoPostBack, when the 'SelectedIndexChanged' server-side event fires and your page PostBack's, you can set the second dropdown's selectedIndex property to the selectedIndex property of the first dropdown. It appears the index's should match since they are loaded from the same row in the datasource table.

Another way that may be quicker for the user would be to use Javascript client-side to do the same thing since you don't have to get any more data from the server.

-Darrin

I have one aspx page where i am databinding two drop down lists based on a dataset (the data is being loaded from an XML Document). The fields get updated properly, no problem there. My question is: "How do you handle the events so that if the user chooses an option in the drop down, it poulates the cooresponding value in the second drop down?"

I have been splitting hairs on this, and I am at a point that I need some help from the community. Here is a look at the code I am using:

**Snip**
 
Right-Click, I mean. Sorry, but I'll bet you figured it out...
Left click on the first dropdown box, and click 'Properties' from the context menu. At the top of the Properties window, click the lightning bolt, which will list server side events. Find the one that says 'SelectedIndexChanged' and double click it. It will create the event with the proper paramaters and will execute server side whenever the combo box index is changed. Make sure the 'AutoPostBack' property of the combobox is set to 'True'.

Also, make sure that anything that you don't want to happen everytime the page postback's is in an if structure:

if(!IsPostBack)
{
//Load combo boxes the first time only here.
}

-Darrin
That is helping a lot...

One quick question on this, what would you recommend for controlling what code is executed on that actual post back then? Right now when the post back occurs is goes through the Page_Load and looks at whether or not it was post (Page.IsPostBack) then executes all the code on the page. If I want to update just a single field, I want to try to avoid all the rest of the code. Any thoughts?

Thanks a bunch.

Matthew.

If the dropdowns are set to AutoPostBack, when the 'SelectedIndexChanged' server-side event fires and your page PostBack's, you can set the second dropdown's selectedIndex property to the selectedIndex property of the first dropdown. It appears the index's should match since they are loaded from the same row in the datasource table.

Another way that may be quicker for the user would be to use Javascript client-side to do the same thing since you don't have to get any more data from the server.

-Darrin

I have one aspx page where i am databinding two drop down lists based on a dataset (the data is being loaded from an XML Document). The fields get updated properly, no problem there. My question is: "How do you handle the events so that if the user chooses an option in the drop down, it poulates the cooresponding value in the second drop down?"

I have been splitting hairs on this, and I am at a point that I need some help from the community. Here is a look at the code I am using:

**Snip**
 
Worked like a charm... THAT was a fine piece of advice.

When I was stepping through the code earlier, seeing that the postback always went through the Page_Load, I didn't even think that there was a option on the form control to change the path that it goes through to process. That alone made what I thought was a very difficult problem, quite the easy solution.

Thanks a ton man.

Matthew.
Right-Click, I mean. Sorry, but I'll bet you figured it out...
Left click on the first dropdown box, and click 'Properties' from the context menu. At the top of the Properties window, click the lightning bolt, which will list server side events. Find the one that says 'SelectedIndexChanged' and double click it. It will create the event with the proper paramaters and will execute server side whenever the combo box index is changed. Make sure the 'AutoPostBack' property of the combobox is set to 'True'.

Also, make sure that anything that you don't want to happen everytime the page postback's is in an if structure:

if(!IsPostBack)
{
//Load combo boxes the first time only here.
}

-Darrin
That is helping a lot...

One quick question on this, what would you recommend for controlling what code is executed on that actual post back then? Right now when the post back occurs is goes through the Page_Load and looks at whether or not it was post (Page.IsPostBack) then executes all the code on the page. If I want to update just a single field, I want to try to avoid all the rest of the code. Any thoughts?

Thanks a bunch.

Matthew.

If the dropdowns are set to AutoPostBack, when the 'SelectedIndexChanged' server-side event fires and your page PostBack's, you can set the second dropdown's selectedIndex property to the selectedIndex property of the first dropdown. It appears the index's should match since they are loaded from the same row in the datasource table.

Another way that may be quicker for the user would be to use Javascript client-side to do the same thing since you don't have to get any more data from the server.

-Darrin

I have one aspx page where i am databinding two drop down lists based on a dataset (the data is being loaded from an XML Document). The fields get updated properly, no problem there. My question is: "How do you handle the events so that if the user chooses an option in the drop down, it poulates the cooresponding value in the second drop down?"

I have been splitting hairs on this, and I am at a point that I need some help from the community. Here is a look at the code I am using:

**Snip**
 
Back
Top