Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Datagrid DropdownList in edit mode???

Reply
Thread Tools

Datagrid DropdownList in edit mode???

 
 
=?Utf-8?B?VGltOjouLg==?=
Guest
Posts: n/a
 
      12-22-2004
Can someone tell me how I get the correct selected office in my drop down
list when I enter edit mode. At the moment it just defaults to the office at
the top of the drop down list rather than picking the office that has
actually been selected previously!

I hope this is clear...

Here is my code...

Thanks

''''''''''''''''''''''Problem - not reading the correct office when in
edit mode
Function GetSelectedIndex(ByVal PageID As String) As Integer

Dim Myconn As New
SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
Dim cmd As New SqlCommand("SelectOffice", Myconn)
cmd.CommandType = CommandType.StoredProcedure

Myconn.Open()

Dim myReader As SqlDataReader = cmd.ExecuteReader()

If myReader.HasRows Then
Do While myReader.Read()
Console.WriteLine(vbTab & "{0}" & vbTab & "{1}",
myReader.GetInt32(0), myReader.GetString(1))
Loop
Else
Console.WriteLine("No rows returned.")
End If



myReader.Close()

Myconn.Close()
End Function

Sub populateDDL_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)

Dim pageID = CInt(CType(e.Item.FindControl("pageID"), TextBox).Text)

'Construct the Database Connection
Dim Myconn As New
SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
Dim cmd As New SqlCommand("SelectOffice", Myconn)

If e.Item.ItemType = ListItemType.EditItem Then

cmd.CommandType = CommandType.StoredProcedure

Myconn.Open()

Dim add_Office As DropDownList

'Populates Office DropDownList with office names
add_Office = CType(e.Item.FindControl("DDLeditOffice"),
DropDownList)
add_Office.DataSource =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
add_Office.DataTextField = "offName"
add_Office.DataValueField = "officeID"
add_Office.DataBind()
add_Office.SelectedIndex = GetSelectedIndex(pageID)
Myconn.Close()
Else

If e.Item.ItemType = ListItemType.Footer Then

cmd.CommandType = CommandType.StoredProcedure

Myconn.Open()

Dim add_Office As DropDownList

'Populates Office DropDownList with office names
add_Office = CType(e.Item.FindControl("DDLaddOffice"),
DropDownList)
add_Office.DataSource =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
add_Office.DataTextField = "offName"
add_Office.DataValueField = "officeID"
add_Office.DataBind()
add_Office.Items.Insert(0, "Select One")
add_Office.Items.FindByText("Select One").Value = 0 'insert
don't create a value, but we need a value during defaults
add_Office.SelectedIndex = 0
Myconn.Close()
End If
End If
End Sub
 
Reply With Quote
 
 
 
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      12-22-2004
Hi Tim,

Don't have time to test your code, but just in case.... this issue is often
caused by incorrectly rebinding to the data on postback. Make sure you are
wrapping the function in If not ispostback

"Tim::.." <myatix_at_hotmail.com> wrote in message
news:(E-Mail Removed)...
> Can someone tell me how I get the correct selected office in my drop down
> list when I enter edit mode. At the moment it just defaults to the office
> at
> the top of the drop down list rather than picking the office that has
> actually been selected previously!
>
> I hope this is clear...
>
> Here is my code...
>
> Thanks
>
> ''''''''''''''''''''''Problem - not reading the correct office when in
> edit mode
> Function GetSelectedIndex(ByVal PageID As String) As Integer
>
> Dim Myconn As New
> SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
> Dim cmd As New SqlCommand("SelectOffice", Myconn)
> cmd.CommandType = CommandType.StoredProcedure
>
> Myconn.Open()
>
> Dim myReader As SqlDataReader = cmd.ExecuteReader()
>
> If myReader.HasRows Then
> Do While myReader.Read()
> Console.WriteLine(vbTab & "{0}" & vbTab & "{1}",
> myReader.GetInt32(0), myReader.GetString(1))
> Loop
> Else
> Console.WriteLine("No rows returned.")
> End If
>
>
>
> myReader.Close()
>
> Myconn.Close()
> End Function
>
> Sub populateDDL_ItemDataBound(ByVal sender As Object, ByVal e As
> System.Web.UI.WebControls.DataGridItemEventArgs)
>
> Dim pageID = CInt(CType(e.Item.FindControl("pageID"),
> TextBox).Text)
>
> 'Construct the Database Connection
> Dim Myconn As New
> SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
> Dim cmd As New SqlCommand("SelectOffice", Myconn)
>
> If e.Item.ItemType = ListItemType.EditItem Then
>
> cmd.CommandType = CommandType.StoredProcedure
>
> Myconn.Open()
>
> Dim add_Office As DropDownList
>
> 'Populates Office DropDownList with office names
> add_Office = CType(e.Item.FindControl("DDLeditOffice"),
> DropDownList)
> add_Office.DataSource =
> cmd.ExecuteReader(CommandBehavior.CloseConnection)
> add_Office.DataTextField = "offName"
> add_Office.DataValueField = "officeID"
> add_Office.DataBind()
> add_Office.SelectedIndex = GetSelectedIndex(pageID)
> Myconn.Close()
> Else
>
> If e.Item.ItemType = ListItemType.Footer Then
>
> cmd.CommandType = CommandType.StoredProcedure
>
> Myconn.Open()
>
> Dim add_Office As DropDownList
>
> 'Populates Office DropDownList with office names
> add_Office = CType(e.Item.FindControl("DDLaddOffice"),
> DropDownList)
> add_Office.DataSource =
> cmd.ExecuteReader(CommandBehavior.CloseConnection)
> add_Office.DataTextField = "offName"
> add_Office.DataValueField = "officeID"
> add_Office.DataBind()
> add_Office.Items.Insert(0, "Select One")
> add_Office.Items.FindByText("Select One").Value = 0 'insert
> don't create a value, but we need a value during defaults
> add_Office.SelectedIndex = 0
> Myconn.Close()
> End If
> End If
> End Sub


 
Reply With Quote
 
 
 
 
=?Utf-8?B?VGltOjouLg==?=
Guest
Posts: n/a
 
      12-22-2004
Hi Ken thanks for the response but I don't think this is the problem!

I would appritiate any other suggestions!

Thanks

"Ken Cox [Microsoft MVP]" wrote:

> Hi Tim,
>
> Don't have time to test your code, but just in case.... this issue is often
> caused by incorrectly rebinding to the data on postback. Make sure you are
> wrapping the function in If not ispostback
>
> "Tim::.." <myatix_at_hotmail.com> wrote in message
> news:(E-Mail Removed)...
> > Can someone tell me how I get the correct selected office in my drop down
> > list when I enter edit mode. At the moment it just defaults to the office
> > at
> > the top of the drop down list rather than picking the office that has
> > actually been selected previously!
> >
> > I hope this is clear...
> >
> > Here is my code...
> >
> > Thanks
> >
> > ''''''''''''''''''''''Problem - not reading the correct office when in
> > edit mode
> > Function GetSelectedIndex(ByVal PageID As String) As Integer
> >
> > Dim Myconn As New
> > SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
> > Dim cmd As New SqlCommand("SelectOffice", Myconn)
> > cmd.CommandType = CommandType.StoredProcedure
> >
> > Myconn.Open()
> >
> > Dim myReader As SqlDataReader = cmd.ExecuteReader()
> >
> > If myReader.HasRows Then
> > Do While myReader.Read()
> > Console.WriteLine(vbTab & "{0}" & vbTab & "{1}",
> > myReader.GetInt32(0), myReader.GetString(1))
> > Loop
> > Else
> > Console.WriteLine("No rows returned.")
> > End If
> >
> >
> >
> > myReader.Close()
> >
> > Myconn.Close()
> > End Function
> >
> > Sub populateDDL_ItemDataBound(ByVal sender As Object, ByVal e As
> > System.Web.UI.WebControls.DataGridItemEventArgs)
> >
> > Dim pageID = CInt(CType(e.Item.FindControl("pageID"),
> > TextBox).Text)
> >
> > 'Construct the Database Connection
> > Dim Myconn As New
> > SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
> > Dim cmd As New SqlCommand("SelectOffice", Myconn)
> >
> > If e.Item.ItemType = ListItemType.EditItem Then
> >
> > cmd.CommandType = CommandType.StoredProcedure
> >
> > Myconn.Open()
> >
> > Dim add_Office As DropDownList
> >
> > 'Populates Office DropDownList with office names
> > add_Office = CType(e.Item.FindControl("DDLeditOffice"),
> > DropDownList)
> > add_Office.DataSource =
> > cmd.ExecuteReader(CommandBehavior.CloseConnection)
> > add_Office.DataTextField = "offName"
> > add_Office.DataValueField = "officeID"
> > add_Office.DataBind()
> > add_Office.SelectedIndex = GetSelectedIndex(pageID)
> > Myconn.Close()
> > Else
> >
> > If e.Item.ItemType = ListItemType.Footer Then
> >
> > cmd.CommandType = CommandType.StoredProcedure
> >
> > Myconn.Open()
> >
> > Dim add_Office As DropDownList
> >
> > 'Populates Office DropDownList with office names
> > add_Office = CType(e.Item.FindControl("DDLaddOffice"),
> > DropDownList)
> > add_Office.DataSource =
> > cmd.ExecuteReader(CommandBehavior.CloseConnection)
> > add_Office.DataTextField = "offName"
> > add_Office.DataValueField = "officeID"
> > add_Office.DataBind()
> > add_Office.Items.Insert(0, "Select One")
> > add_Office.Items.FindByText("Select One").Value = 0 'insert
> > don't create a value, but we need a value during defaults
> > add_Office.SelectedIndex = 0
> > Myconn.Close()
> > End If
> > End If
> > End Sub

>
>

 
Reply With Quote
 
Saravana
Guest
Posts: n/a
 
      12-22-2004
Check out "Displaying a Drop-Down List in Edit Mode" section in this
article,
http://msdn.microsoft.com/library/en...ngmultiplerows


--
-Saravana
http://dotnetjunkies.com/WebLog/saravana/
www.ExtremeExperts.com



"Tim::.." <myatix_at_hotmail.com> wrote in message
news:(E-Mail Removed)...
> Can someone tell me how I get the correct selected office in my drop down
> list when I enter edit mode. At the moment it just defaults to the office

at
> the top of the drop down list rather than picking the office that has
> actually been selected previously!
>
> I hope this is clear...
>
> Here is my code...
>
> Thanks
>
> ''''''''''''''''''''''Problem - not reading the correct office when in
> edit mode
> Function GetSelectedIndex(ByVal PageID As String) As Integer
>
> Dim Myconn As New
> SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
> Dim cmd As New SqlCommand("SelectOffice", Myconn)
> cmd.CommandType = CommandType.StoredProcedure
>
> Myconn.Open()
>
> Dim myReader As SqlDataReader = cmd.ExecuteReader()
>
> If myReader.HasRows Then
> Do While myReader.Read()
> Console.WriteLine(vbTab & "{0}" & vbTab & "{1}",
> myReader.GetInt32(0), myReader.GetString(1))
> Loop
> Else
> Console.WriteLine("No rows returned.")
> End If
>
>
>
> myReader.Close()
>
> Myconn.Close()
> End Function
>
> Sub populateDDL_ItemDataBound(ByVal sender As Object, ByVal e As
> System.Web.UI.WebControls.DataGridItemEventArgs)
>
> Dim pageID = CInt(CType(e.Item.FindControl("pageID"),

TextBox).Text)
>
> 'Construct the Database Connection
> Dim Myconn As New
> SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
> Dim cmd As New SqlCommand("SelectOffice", Myconn)
>
> If e.Item.ItemType = ListItemType.EditItem Then
>
> cmd.CommandType = CommandType.StoredProcedure
>
> Myconn.Open()
>
> Dim add_Office As DropDownList
>
> 'Populates Office DropDownList with office names
> add_Office = CType(e.Item.FindControl("DDLeditOffice"),
> DropDownList)
> add_Office.DataSource =
> cmd.ExecuteReader(CommandBehavior.CloseConnection)
> add_Office.DataTextField = "offName"
> add_Office.DataValueField = "officeID"
> add_Office.DataBind()
> add_Office.SelectedIndex = GetSelectedIndex(pageID)
> Myconn.Close()
> Else
>
> If e.Item.ItemType = ListItemType.Footer Then
>
> cmd.CommandType = CommandType.StoredProcedure
>
> Myconn.Open()
>
> Dim add_Office As DropDownList
>
> 'Populates Office DropDownList with office names
> add_Office = CType(e.Item.FindControl("DDLaddOffice"),
> DropDownList)
> add_Office.DataSource =
> cmd.ExecuteReader(CommandBehavior.CloseConnection)
> add_Office.DataTextField = "offName"
> add_Office.DataValueField = "officeID"
> add_Office.DataBind()
> add_Office.Items.Insert(0, "Select One")
> add_Office.Items.FindByText("Select One").Value = 0

'insert
> don't create a value, but we need a value during defaults
> add_Office.SelectedIndex = 0
> Myconn.Close()
> End If
> End If
> End Sub



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Snapshot restraint - edit, edit, edit Alan Browne Digital Photography 24 05-10-2005 10:15 PM
Snapshot restraint - edit, edit, edit Patrick Digital Photography 0 05-06-2005 10:53 PM
Edit All Function for DataGrid, and Moving the Edit Function in a DataGrid Schultz ASP .Net 3 02-14-2005 04:47 AM
To all Gurus: How can I edit/update a DataGrid in a DataGrid (nested DataGrid)? Possible? Andreas Klemt ASP .Net Datagrid Control 0 10-08-2003 01:19 AM
copying value of DDL in a Datagrid "pre-edit command" to value in "post edit command" San Diego Guy ASP .Net 0 08-07-2003 08:59 PM



Advertisments