开发者

Dynamically loading controls and postbacks

On a dashboard page I'm loading several controls dynamically; the controls always need to be loaded so there is no if (!Page.IsPostBack) code. 开发者_开发问答The problem though is in that in the code-behind, we are using Request.Form.GetValues and a particular dropdown field's value is always null (presumably because the controls are being cleared and reloaded on a postback) and this is triggering an error later on in the page that expectes a value from this dropdown. I have to use Request.Form.GetValues because the page enables dynamically adding rows (each row containing 5+ HTML inputs) via Javascript.

If I check for postback, then the button event of the control does not fire at all but instead the dashboard page is reloaded, and no controls are loaded at all (i.e. the page is blank).

How do I get around this issue? I need to dynamically load the controls at all times, but I also need to retrieve the values via Request.Form.GetValues. Any suggestions?

EDIT

Here's what the code looks like, roughly:

(Client-side HTML)

<table>
    <tr>
        <td align="center">
            <select id="Class1" class="txtboxcomm" name="Class1" runat="server" />
            <a href="javascript:classFinder('1')">&nbsp;Look Up</a>
        </td>
   </tr>    
</table>
<div id="action">
    <asp:LinkButton ID="ibtnGetQuote" runat="server" OnClick="ibtnGetQuote_Click" CssClass="getQuote" AccessKey="G">Get Quote</asp:LinkButton>
</div>

(Server-side, in ibtnGetQuote_Click - this is actually a loop since user can append multiple rows; "i" is appended to the word "Class" to find the exact row)

IList<string> classes = this.GetFormValues("Class1");

// This will return null all the time...
private IList<string> GetFormValues(string clientID)
{
    String[] values;
    values = Request.Form.GetValues(clientID);
    if (values == null)
    {
        Control ctl = FindControl(clientID); // uses built-in FindControl method
        values = Request.Form.GetValues(ctl.ClientID);
    }
    return values;
}

The original code was hardcoding the "ctl00$xx$xx" stuff, but the control may or may not be loaded so we cannot guarantee the name the control will get. In fact, I'm not even sure if this will work for all scenarios since only the FIRST control is a server control, the rest are just standard HTML controls so wouldn't have a "ClientID".


It seems you are loading the controls in Load event. This is too late as by this time the viewstate is already processed. You need to load the controls in Init event.


What might be a good idea is to eagerly load the controls as early as possible to add them to the control tree. If you can do this before the LoadViewState method is fired, the controls should process the view state like any statically declared controls would.


Do control creation and population in the CreateChildControls event. Call EnsureChildControls() before you check the form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜