开发者

Cant set drop down list selected value in page load

开发者 https://www.devze.com 2023-01-02 15:47 出处:网络
I\'m trying to set the selected value of a ddl during开发者_如何学Go page load ie. before databind.

I'm trying to set the selected value of a ddl during开发者_如何学Go page load ie. before databind.

This causes "selected value does not exist" errors. So I force a databind, and add a new element if it does not exist in the data source.

However it looks like when the databind is performed later in the page lifecycle that my added element(s) are removed/overwritten.

Am I setting the values in the wrong part of the life cycle? what I'm doing seems rather hackish and I think im going about this the wrong way... is there a better way to do this?


Dont do it on PageLoad do it on the DataBound event of the ddl


Did you consider the OnPreRender event of the DDL... I think you will have everything to set the selected value there


However it looks like when the databind is performed later in the page lifecycle that my added element(s) are removed/overwritten.

That is to be expected, databinding clears out the items and rebinds them again. You should look at what points in the page lifecycle you are calling DataBind and also attempting to set the Selected Value.

Have you considered Page_PreRender to set the SelectedValue? This fires after all the initialisation is done, last thing before the page is rendered to the browser. (Hopefully you won't be doing any databinding in Page_PreRender ;))

But it does not seem very logical to be setting the SelectedValue in one place only for it to be overwritten again, you should only be setting the SelectedValue once - after the final .DataBind()


However it looks like when the databind is performed later in the page lifecycle that my added element(s) are removed/overwritten.

As bgs264 says, that's the behaviour of databinding by design. However, if you set the AppendDataBoundItems attribute to true for your DropDownList, this won't happen, your manually added item will remain in place.

<asp:DropDownList runat="server" id="MyDropDownList" AppendDataBoundItems="true" />


My work around to this solution is as follows:

In Page Load:

Page_Load(..)
{
  if(...)
  {
      hidCGroup.value = objCG.CName;
  }
}

In DataBound:

ddlContGroup_DataBound(..)
{
  ddlContGroup.Items.Insert(0, "--Select--");
  ddlContGroup.SelectedIndex = ddlContGroup.Items.IndexOf(ddlContGroup.Items.FindByText(hidCGroup.Value));
}

Now there are two things to take care of. When you are using FindByText and FindByValue always take special care of the value which you are selecting from the ddl.

Sometimes, we use a numeric item as the DataValue and a text item as the DataText, when that happens, you need to interchange FindByText and FindByValue so that the proper selection is made.

Hope this helps.


ddlExample.SelectedIndex=ddlExample.Items.IndexOf(ddlExample.Items.FindByValue(ExampleID.ToString()));

or

ddlExample.SelectedIndex=ddlExample.Items.IndexOf(ddlExample.Items.FindByText(ExampleText.ToString()));
0

精彩评论

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