By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,026 Members | 975 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,026 IT Pros & Developers. It's quick & easy.

(C#) DropDownList Using SelectedIndex w/ onDataBinding

P: 42
I have a DataGrid and a column that changes into a DropDownList when edited:

Expand|Select|Wrap|Line Numbers
  1. <columns>
  2.     <asp:templatecolumn headertext="Role Type">
  3.         <itemtemplate>
  4.             <asp:hyperlink id="hlinkRoleType" navigateurl='<%# "contact_roles_details.aspx?id=" + DataBinder.Eval(Container, "DataItem.ID", "{0}").ToString() %>' Text='<%# DataBinder.Eval(Container, "DataItem.Role", "{0}").ToString() %>' runat="server"/>
  5.         </itemtemplate>
  6.         <edititemtemplate>
  7.             <asp:dropdownlist id="ddlRoleEdit" datavaluefield="ID" datatextfield="Name" datasource="<%# FillRoleList() %>" ondatabinding="ddlRoleEdit_ItemDataBound" runat="server" />
  8.         </edititemtemplate>
  9.     </asp:templatecolumn>
  10.     <asp:editcommandcolumn buttontype="LinkButton" edittext="&lt;img src=&quot;images/edit_icon.gif&quot;&gt;" />
  11.     <asp:buttoncolumn text="&lt;img src=&quot;images/delete_icon.gif&quot;&gt;" commandname="deleteRole"></asp:buttoncolumn>
  12. </columns>
I've spent many many hours trying to set the default value of the DDL to whatever was present before editing. Everything I've inserted into the *** below stirs up some petty technical error in C#:

Expand|Select|Wrap|Line Numbers
  1. public void ddlRoleEdit_ItemDataBound(Object sender, DataGridItemEventArgs e)
  2. {
  3.     DropDownList ddl = (DropDownList) sender;
  5.     ddl.SelectedValue = ***
  6. }
Dec 10 '07 #1
Share this Question
Share on Google+
1 Reply

P: 42
Still having no luck. In addition, I used the wrong EventArgs type, even though I swear it worked at one point. The corrected C# code is below. With plain old EventArgs, it's even harder than before to do what I want.

Expand|Select|Wrap|Line Numbers
  1. public void ddlRoleEdit_ItemDataBound(Object sender, EventArgs e)
  2. {
  3.       DropDownList ddl = (DropDownList) sender;
  4.       ddl.SelectedValue = ***
  5. }
Dec 12 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.