开发者

Setting selected value in HTML drop down

I have got a drop down in my aspx page which I have given a data source to in the code behind. I am trying to set the selected value, but it doesn't show the option that I set it to. Sorry for the rubbish explanation. I will try with the code I've been using.

So this is the front end part:

        <select id="OffenceClassificationDropDown" runat="server" style="white-space:pre;"
                onchange="StoreCrimeChange(internalCrimeReference, 'OffenceClassification', this.value)">
        </select>

And this is the code behind part where I bind the data and try to set the selected value:

            this.OffenceClassificationDropDown.DataSource = ManageLookups.GetOffenceClassifications(Session, false);
            this.OffenceClassificationDropDown.DataTextField = "Description";
        开发者_高级运维    this.OffenceClassificationDropDown.DataValueField = "Id";
            this.OffenceClassificationDropDown.DataBind();

            if (!String.IsNullOrEmpty(this.crime.OffenceClassification.Description))
            {
                this.OffenceClassificationDropDown.Items.FindByValue(this.crime.OffenceClassification.Id.ToString()).Selected = true;
            }

The reason for changing the value to the selected one is because I have two panels which users switch between. The can go back to the original page and then to this one, which should retain the valus they have selected.

Hopefully someone can tell me why this isn't working because it's driving me bonkers! :)

Thank you all in advance!


Why dont you use the asp:DropDownList it has a selectedvalue property built into it and essentially it will render out to the same thing you have here


Your code looks OK, except you may want to also check to ensure that the FindByValue() does not return a null prior to setting its Selected property to avoid getting ORNS exceptions.

The selected option should certainly change if this event is fired and if the FindByValue finds the item and if another piece of code does not change it again to something else.

Which event is this code in? Are you initiating a postback when the panels switch?

You may want to set a breakpoint at the crime.OffenceClassification.Description and try to step through your code in debugger to ensure that the value of crime.OffenceClassification.Description is always set to what you expect it to be. Also this will help you make sure that the code is actually being hit.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜