问题描述:

I have the dropdownlist in the gridview itemtemplate, i need to select the value based on the dataset, i tried to bind it as "SelectedValue='<%# Eval("code") %>', but i cant find any attribute like "SelectedValue" in HTML code.

I tried below link also it was not work out to me

Eval() in a DropDownList within a GridView

Can any one help me in that

网友答案:

You have to understand how dropdown list (select tag) in HTML works. E.g. if you want to select some item you have to mark it as selected as follows

<select>
  <option value="a">a</option>
  <option value="b" selected="1">b</option>
  <option value="c">c</option>
</select>

So you have to put selected="1" to item you want to select. That means you can not do it easily using Eval method. You have to utilize server side which will do it for you.

The example given on the page you have postetd and which you said you have tried works correctly. Check whether the HTML output contains value attributes at each option. The value can differ from what is enclosed within the option tag. If the value attribute is missing that is the reason why the item is not being selected. (Use firebug or any developer console to examine).

相关阅读:
Top