Wednesday, March 21, 2012

Question: Firing two requests with one user interaction.

I have two DropDownList controls (ddl1 and ddl2) on my webpage and a GridView control. When the user makes a selection in ddl1, I should retrieve values for ddl2 and also update the GridView control using the selected value. My GridView takes about 5 to 6 secs to load, in the mean time the ddl2 shows the old items, it gets updated only when the GridView load is complete (i.e. the server returns the result). Is there a way that I update the ddl2 on the screen first and then fire a request from within the code and update the GridView.

In a nutshell, can I initiate two consecutive requests for one user selection on the UI?, and can I update the page after the 1st request is done and before the second request is fired?

Thanks.

Beware of annoying the user, but...

Yes. Set a commandargument for your button if not is postback. The command argument would be "DDL". When the user clicks, check the command argument. if it is one, do the ddl update, write a javascript to the page using the clientscript getpostbackreference (see the msdn page for more info). Update the commandargument on the button on the way back out. The javascript should essentially call the button using the postbackreference. When the button event is handled, reset the command argument.

Good luck, and let me know if this helps by setting the post as answered.

--JJ


Place ddl2 and the gridview each in their own updatepanel with UpdateMode set to "Conditional". Then set ddl1 as an AsynchPostBackTrigger for both updatepanels. They should then each update at their own pace.


The problem with that is, both the ddl2 and GridView get updated only at the same time. My intention is to update the ddl2 on the browser and then later update the gridview. i.e. the ddl2's change should be immediate and may not wait for the GridView to get updated (which might take longer as the DataBind for the GridView takes a bit longer).

This might involve two separate requests behind the scenes after the user selection in ddl1 changes.

Thanks.


I don't a have button control, I have two dropdowns (ddl1 and ddl2) and one GridView control.

Once the selection in the ddl changes, my intention is to update the ddl2 on the browser (needs values from a SQL Database) and then later update the gridview. i.e. the ddl2's change should be immediate and may not wait for the GridView to get updated (which might take longer as the DataBind for the GridView takes a bit longer).

This might involve two separate requests behind the scenes after the user selection in ddl1 changes. And the question is how?

Thanks.


As before, place ddl2 and the gridview in their own updatepanels with UpdateMode="Conditional". Set ddl1 as an AsynchPostBackTrigger for ddl2's updatepanel. That will cause it to update whenever ddl1 changes (make sure you have autopostback="true" on ddl1).

Now, in the ddl2_DataBound event in the code-behind, call the Update() method of the gridview's updatepanel.


If you are not using AJAX, you can use the DDL on selected index changed event in the same way that I described.

--JJ


But there isn't a command argument you can tie to in on the dropdown list. Create a viewstate field to store the "step" of the process you are working on.

Viewstate("DoWhat") = "DDL"

for the page load when it is not a postback

Viewstate("Dowhat") = "GridView"

for the postback if you just handled the DDL refresh.

Here is some code with labels being the refresh targets.

public partialclass doublepostback : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e) {if (!IsPostBack) { ViewState["DoWhat"] ="first"; }else {if ((string)ViewState["DoWhat"] =="second") { doNext(); } } }private void doNext() { Label2.Text ="Set the Second one"; ViewState["DoWhat"] ="first"; mybody.Attributes.Remove("onload"); }protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) {string script; script = @."<script language=""javascript"" type=""text/javascript""> function doNext(){ " + ClientScript.GetPostBackEventReference(Page,"reposting") + @."} </script> "; ClientScript.RegisterClientScriptBlock(typeof(System.String),"repost", script); Label1.Text ="Set the first one"; ViewState["DoWhat"] ="second"; mybody.Attributes.Add("onLoad","doNext();"); }}

No comments:

Post a Comment