Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

How to force a postback from code

I have 2 dropdownlists bound to datasources in a 2.0 asp.net page.

When the first one changes, the second one does as well because the first control is a parameter in the datasource of the second. When the pages is first loaded, I want to set the value of the first list to a cookie value.

That works, but it isn't causing a postback to update the second list as it does when the user makes a selection. How can I force the first control to do a postback and update the datasource of the second?

TIA,

Steve

0
km5gn
Asked:
km5gn
2 Solutions
 
kdtreshCommented:
Select the item and look in the properties pane for "AutoPostBack" and set it to true. That should postback every time the user selects a different item.
0
 
kdtreshCommented:
Another way to do it is in to add AutoPostBack="True" to your asp:DropDownList tag.

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"> should do it.
0
 
silemoneCommented:
why not...in the PageLoad...ControlName.AutoPostBack = true

forgot that one kdtresh...but kdtresh is right and should get points...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
km5gnAuthor Commented:
Perhaps I wasn't clear. Autopostback is set to true and all works fine when the user makes a selection. It doesn't happen when I set the selection in code. I want it to behave the same way when I set the selection in code as it does when the user makes the selection.
0
 
silemoneCommented:
when you do it in code, are you doing it form an event?
0
 
BTosonCommented:
If you are doing it from code you will have to manually fire off the event handler.
0
 
silemoneCommented:
If not, you will either have to call an event
0
 
kdtreshCommented:
One possible solution here: http://forums.asp.net/t/928411.aspx

C#:
----
StringBuilder sbScript = new StringBuilder();

sbScript.Append("<script language='JavaScript' type='text/javascript'>\n");
sbScript.Append("<!--\n");
sbScript.Append(this.GetPostBackEventReference(this, "PBArg") + ";\n");
sbScript.Append("// -->\n");
sbScript.Append("</script>\n");

this.RegisterStartupScript("AutoPostBackScript", sbScript.ToString());

VB:
----
Dim sbScript As New StringBuilder()

sbScript.Append("<script language='JavaScript' type='text/javascript'>" + ControlChars.Lf)
sbScript.Append("<!--" + ControlChars.Lf)
sbScript.Append((Me.GetPostBackEventReference(Me, "PBArg") + ";" + ControlChars.Lf))
sbScript.Append("// -->" + ControlChars.Lf)
sbScript.Append("</")
sbScript.Append("script>" + ControlChars.Lf)

Me.RegisterStartupScript("AutoPostBackScript", sbScript.ToString())
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now