English 中文(简体)
How to set DropDownList selected item in markup?
原标题:

I have a DropDownList in a template column of a GridView control. The GridView is bound to a list of objects. Each object has a property of type int which corresponds to a value in one of the DropDownLists ListItems. I could set the selected item programatically by adding a DataBind event to the drop down, but I m wondering if there s a way to set the selected item by using a code block in the aspx markup.

最佳回答

Be cautious in this design. To create grid drop downs in this manner means that for every option in a drop down, you are going to be repeating for every single row. This can very quickly added up to page sizes that are over a MB if you have more than a few rows or multiple drop down columns, which will degrade performance.

That being said, you can do this in the mark up by using the context binding script tags:

<asp:DropDown id="dropDown1" SelectedValue= <%# Eval("Key") %>  runat="server"/>

The context binding tags also let you call public/protected functions on the page/user control as:

<asp:DropDown id="dropDown1" SelectedValue= <%# myFunction((int) Eval("Key")) %>  runat="server"/>

public string myFunction(int key){
  return key.ToString();
}

As an alternative to producing the same repetitive HTML for every row, you could make those drop downs autocompleters or create a hidden drop down that only renders the HTML once and then uses JQuery or JavaScript to populate all your grid drop downs clientside.

问题回答

You can set it in the markup via:

<ItemTemplate>
<asp:DropDown .. SelectedValue= <%# Eval("Key") %>  />
</ItemTemplate>

Depends on how you bind it, are you using a data source control? Whatever the case, I have noticed that this approach it may try to set the value before the items get bound and that may throw an exception. Not sure, had that happen once, thought it may be that, but I should have looked into it more in-depth.

HTH.

you can also use RowDataBound event of GridView or you can select in markup as described by @Brian





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签