Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET VB.NET
Hello
 
On my webpage when I edit a row from the gridview, a dropdownlist is used to select a value. Now the default selected value is the first value from the list provided by the datasource.
The problem I have is that I can't set the selected value for the DDL.
 
In my vb code I try to set the selected value as follows:
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles GridView1.RowEditing
    Dim row As GridViewRow
    Dim lstdrop As DropDownList
    row = GridView1.Rows.Item(e.NewEditIndex())
    lstdrop = row.FindControl("DropDownListOwner")
    lstdrop.SelectedIndex = 3
End Sub
 
Here is a part of my asp code:
<EditItemTemplate>
    <asp:DropDownList ID="DropDownListOwner" runat="server" DataSourceID="SqlDataOwner" DataValueField="User_name" DataTextField="User_name" AutoPostBack="true">
    </asp:DropDownList>
</EditItemTemplate>
 
When clicking edit on a row the following error occurs:
System.NullReferenceException: Object reference not set to an instance of an object
Posted 29-Mar-11 0:41am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I think this might help.
 
Try adding the following values to the EditItemTemplate DropDownList item.
 
SelectedValue='<%#Bind("value")%>' AppendDataBoundItems=true
  Permalink  
Comments
tomtimmers at 29-Mar-11 9:02am
   
I've changed it to:
<asp:DropDownList ID="DropDownListOwner" runat="server" DataSourceID="SqlDataOwner" DataValueField="UserID" DataTextField="User_name" AutoPostBack="true" AppendDataBoundItems="True" SelectedValue='<%# Bind("User_name") %>'>
And now I get the error: "'System.Data.DataRowView' does not contain a property with the name 'User_name'"
Another thing is that in Microsoft Visual Web Developer 2010 Express the property 'SelectedValue' isn't suggested. Is this normal?
tomtimmers at 29-Mar-11 9:07am
   
Is it possible to set the SelectedValue to a value from the list? Like this:
<asp:DropDownList ID="DropDownListOwner" runat="server" DataSourceID="SqlDataOwner" DataValueField="UserID" DataTextField="User_name" AutoPostBack="true" AppendDataBoundItems="True" SelectedValue="ttimmers">
If I do this I get the error:
The 'SelectedValue' property cannot be set declaratively
tomtimmers at 30-Mar-11 4:50am
   
Your suggestion works.
I modified the DDL as follows:
<asp:DropDownList ID="DropDownListOwner" runat="server" DataSourceID="SqlDataOwner" DataValueField="User_name" DataTextField="User_name" AutoPostBack="true" AppendDataBoundItems="true" SelectedValue='<%#Bind("Pool_IP_owner")%>'>
Member 9124526 at 10-Oct-14 0:04am
   
Hi
 
I am facing same Issue, how you got SelectedValueProperty in DropDownList as I am not finding the SelectedValue property
harsha narayana at 30-Mar-11 12:12pm
   
Good to know that the suggestion helped you :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Not sure if it matters, but try removing the parenthesis on NewEditIndex
  Permalink  
Comments
tomtimmers at 29-Mar-11 5:55am
   
Thanks for the suggestion, but the result is the same.
walterhevedeich at 29-Mar-11 6:04am
   
Are you using master page/user control(ascx) for this one?
tomtimmers at 29-Mar-11 7:22am
   
I'm not using ascx. I have a User_ReleaseIP.aspx file that uses a User_ReleaseIP.aspx.vb file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 455
1 Sergey Alexandrovich Kryukov 312
2 DamithSL 179
3 King Fisher 179
4 Peter Leow 154
0 OriginalGriff 240
1 Maciej Los 95
2 Sascha Lefévre 85
3 King Fisher 70
4 F-ES Sitecore 55


Advertise | Privacy | Mobile
Web03 | 2.8.150327.1 | Last Updated 29 Mar 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100