Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting a preselected value in a dropdownlist. 1

Status
Not open for further replies.

joeb55

Programmer
Dec 10, 2001
1
US
Having a hard time getting a preselected value in a dropdownlist.

The two lines I am questioning are:

tb_TaskOwner.SelectedIndex = tb_TaskOwner.Items.IndexOf(tb_TaskOwner.Items.FindByValue("TSO"))
tb_TaskGroup.SelectedIndex = tb_TaskGroup.Items.IndexOf(tb_TaskGroup.Items.FindByValue("TSG"))




I have no problem righting the values for "TSO" & "TSG" to a label but i cannot pass the value back to the dropdownlists "tb_TaskOwner" & "tb_TaskGroup" as selected value when I modified my form.

Cannot get it to preselect!

Code as follows:
Here is the AM.aspx

Start------------------------------------------------------------------------------------

<%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;AM.aspx.vb&quot; Inherits=&quot;MSDDTask.AM&quot;%>
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<HTML>
<HEAD>
<title>AM_Tasks</title>
<LINK href=&quot;ToolsStyle1.css&quot; type=&quot;text/css&quot; rel=&quot;stylesheet&quot;>
</HEAD>
<body MS_POSITIONING=&quot;FlowLayout&quot;>
<TABLE id=&quot;Table1&quot; cellSpacing=&quot;1&quot; cellPadding=&quot;1&quot; width=&quot;100%&quot; border=&quot;0&quot;>
<TR>
<TD width=&quot;100%&quot; colSpan=&quot;3&quot; rowSpan=&quot;1&quot;></TD>
</TR>
<TR>
<TD style=&quot;WIDTH: 353px&quot; vAlign=&quot;top&quot;>
<P> </P>
</TD>
<TD> </TD>
<TD vAlign=&quot;top&quot; width=&quot;100%&quot;>
<form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;>
<P>
<blockquote>
<P class=&quot;PageTitle&quot;><asp:label id=&quot;lblTitle&quot; runat=&quot;server&quot;></asp:label></P>
<P class=&quot;PageTitle&quot;>
<asp:Label id=&quot;lblOwner&quot; runat=&quot;server&quot;></asp:Label></P>
<P class=&quot;PageTitle&quot;>
<asp:Label id=&quot;lblGroup&quot; runat=&quot;server&quot;></asp:Label></P>
<P><asp:label class=&quot;FieldDescriptions&quot; id=&quot;lb_TaskOwner&quot; runat=&quot;server&quot;>Task Owner</asp:label><BR>
<asp:dropdownlist id=&quot;tb_TaskOwner&quot; runat=&quot;server&quot; DataTextField=&quot;TaskOwnerName&quot; DataValueField=&quot;TaskOwnerName&quot;></asp:dropdownlist><asp:requiredfieldvalidator class=&quot;ValidationDescriptions&quot; id=&quot;vld_TaskOwner&quot; runat=&quot;server&quot; ErrorMessage=&quot;You must select a Task Owner!&quot; ControlToValidate=&quot;tb_TaskOwner&quot;>*</asp:requiredfieldvalidator></P>
<P><asp:label class=&quot;FieldDescriptions&quot; id=&quot;lblTaskGroup&quot; runat=&quot;server&quot;>Task Group</asp:label><BR>
<asp:dropdownlist id=&quot;tb_TaskGroup&quot; runat=&quot;server&quot; DataTextField=&quot;TaskGroupName&quot; DataValueField=&quot;TaskGroupName&quot;></asp:dropdownlist><asp:requiredfieldvalidator class=&quot;ValidationDescriptions&quot; id=&quot;vld_TaskGroup&quot; runat=&quot;server&quot; ErrorMessage=&quot;You must select a Task Group!&quot; ControlToValidate=&quot;tb_TaskGroup&quot;>*</asp:requiredfieldvalidator></P>
<P></P>
<P><asp:button id=&quot;btnModify&quot; runat=&quot;server&quot; Text=&quot;Modify Delivery&quot;></asp:button> 
<asp:button id=&quot;btnAddNew&quot; runat=&quot;server&quot; Text=&quot;Add New Delivery&quot;></asp:button> 
<asp:button id=&quot;btnCancel&quot; runat=&quot;server&quot; Text=&quot;Cancel&quot;></asp:button><BR>
 </P>
<asp:validationsummary id=&quot;VldSummary&quot; runat=&quot;server&quot;></asp:validationsummary>
<P></P>
<P> </P>
</blockquote>
</form>
</FONT></TD>
</TR>
<TR>
<TD style=&quot;WIDTH: 353px&quot; colSpan=&quot;3&quot;></TD>
</TR>
</TABLE>
</body>
</HTML>


End of AM.aspx------------------------------------------------------------------



and here is the Code Behind AM.aspx.vb

Start------------------------------------------------------------------------------------


Imports System
Imports System.Data
Imports System.Web.UI
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.ListControl
Imports System.Web.UI.HtmlControls

Public Class AM
Inherits System.Web.UI.Page
Protected WithEvents lb_TaskOwner As System.Web.UI.WebControls.Label
Protected WithEvents tb_TaskOwner As System.Web.UI.WebControls.DropDownList
Protected WithEvents vld_TaskOwner As System.Web.UI.WebControls.RequiredFieldValidator
Protected WithEvents lblTaskGroup As System.Web.UI.WebControls.Label
Protected WithEvents tb_TaskGroup As System.Web.UI.WebControls.DropDownList
Protected WithEvents vld_TaskGroup As System.Web.UI.WebControls.RequiredFieldValidator
Protected WithEvents Form1 As System.Web.UI.HtmlControls.HtmlForm
Protected WithEvents lblTitle As System.Web.UI.WebControls.Label
Protected WithEvents btnModify As System.Web.UI.WebControls.Button
Protected WithEvents btnAddNew As System.Web.UI.WebControls.Button
Protected WithEvents btnCancel As System.Web.UI.WebControls.Button
Protected WithEvents VldSummary As System.Web.UI.WebControls.ValidationSummary
Protected WithEvents lblOwner As System.Web.UI.WebControls.Label
Protected WithEvents lblGroup As System.Web.UI.WebControls.Label
Dim strConn As String = CStr(ConfigurationSettings.AppSettings(&quot;CSSCConn&quot;))
Dim MyConnOwner As New SqlConnection(strConn)
Dim MyConn As New SqlConnection(strConn)
Dim NTU As String


#Region &quot; Web Form Designer Generated Code &quot;

'This call is required by the Web Form Designer.



Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

NTU = LCase(Request.ServerVariables(&quot;LOGON_USER&quot;))
If (Request.QueryString(&quot;TS_UI&quot;)) = &quot;&quot; Then
ClearData()
ddlTaskOwner()
ddlTaskGroup()

Else
BindData()
If Not Page.IsPostBack Then
btnModify.Visible = True
btnAddNew.Visible = False
ddlTaskOwner()
ddlTaskGroup()

End If
End If
End Sub

Sub ddlTaskOwner()
Dim SQLOwner As String = &quot;Select Distinct TaskOwnerName from V_TaskOwner&quot;
Dim objDROwner As SqlDataReader
Dim CmdOwner As New SqlCommand(SQLOwner, MyConnOwner)

MyConnOwner.Open()
objDROwner = CmdOwner.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
tb_TaskOwner.DataSource = objDROwner
tb_TaskOwner.DataBind()
tb_TaskOwner.Items.Insert(0, New ListItem(&quot;Select a Task Owner&quot;, &quot;&quot;))
End Sub

Sub ddlTaskGroup()
Dim SQLGroup As String = &quot;select distinct TaskGroupName from TaskGroup&quot;
Dim objDRGroup As SqlDataReader
Dim CmdGroup As New SqlCommand(SQLGroup, MyConn)

MyConn.Open()
objDRGroup = CmdGroup.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
tb_TaskGroup.DataSource = objDRGroup
tb_TaskGroup.DataBind()
tb_TaskGroup.Items.Insert(0, New ListItem(&quot;Select a Group&quot;, &quot;&quot;))
End Sub


Public Sub BindData()
Dim strSQL2 As String = &quot;select * from TaskSched where TS_UI = '&quot; + (Request.QueryString(&quot;TS_UI&quot;)) + &quot;'&quot;
Dim Conn As New SqlConnection(strConn)

Dim Cmd2 As New SqlCommand(strSQL2, Conn)
Dim TS_UI As String
Dim drMod As SqlDataReader

Conn.Open()
drMod = Cmd2.ExecuteReader
drMod.Read()

lblTitle.Text = drMod(&quot;TS_Group&quot;)

Dim TSO As String
Dim TSG As String

TSO = drMod(&quot;TS_Customer&quot;)
lblOwner.Text = TSO

TSG = drMod(&quot;TS_Group&quot;)
lblGroup.Text = TSG

tb_TaskOwner.SelectedIndex = tb_TaskOwner.Items.IndexOf(tb_TaskOwner.Items.FindByValue(&quot;TSO&quot;))
tb_TaskGroup.SelectedIndex = tb_TaskOwner.Items.IndexOf(tb_TaskGroup.Items.FindByValue(&quot;TSG&quot;))

Conn.Close()
'Response.Write(strSQL2)

End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Server.Transfer(&quot;Admin_TaskByOwner.aspx&quot;)
End Sub

Private Sub ClearData()
tb_TaskOwner.DataValueField = &quot;&quot;
tb_TaskGroup.DataValueField = &quot;&quot;

btnModify.Visible = False
btnAddNew.Visible = True

End Sub

Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click
Dim CustUpdateQuery As String
CustUpdateQuery = &quot;Update TaskSched Set &quot;
CustUpdateQuery &= &quot;TS_Customer='&quot; & tb_TaskOwner.DataValueField & &quot;', &quot;
CustUpdateQuery &= &quot;TS_Group='&quot; & tb_TaskGroup.DataValueField & &quot;' &quot;
CustUpdateQuery &= &quot;Where TS_UI='&quot; & (Request.QueryString(&quot;TS_UI&quot;)) & &quot;'&quot;

Dim strConnectionString As String
Dim Config As Hashtable

strConnectionString = CStr(ConfigurationSettings.AppSettings(&quot;CSSCConn&quot;))


Dim pConn As New SqlConnection(strConnectionString)
Dim cmd As New SqlCommand()
cmd.CommandText = CustUpdateQuery
cmd.Connection = pConn
Response.Write(CustUpdateQuery)
Try
pConn.Open()
cmd.ExecuteNonQuery()
'lblOK.Visible = True

Catch err As Exception

End Try
pConn.Close()
Server.Transfer(&quot;Admin_TaskByOwner.aspx&quot;)

End Sub

Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
Response.Write(&quot;Add New Data&quot;)
Dim CustInsertQuery As String
CustInsertQuery = &quot;Insert Into TaskSched (&quot;
CustInsertQuery &= &quot;TS_Customer, TS_Group) &quot;
CustInsertQuery &= &quot;Values ('&quot;
CustInsertQuery &= tb_TaskOwner.DataValueField & &quot;', '&quot;
CustInsertQuery &= tb_TaskGroup.DataValueField & &quot;', '&quot;

Dim strConnectionString As String
Dim Config As Hashtable

strConnectionString = CStr(ConfigurationSettings.AppSettings(&quot;CSSCConn&quot;))

End Sub

Private Sub InitializeComponent()

End Sub
End Class


End of AM.aspx.vb------------------------------------------------------------------


Thanks
 
Please don't post all of the code next time just the relevent code. Thx

Anyway this is what you have


Dim TSO As String
Dim TSG As String

TSO = drMod(&quot;TS_Customer&quot;)
lblOwner.Text = TSO

TSG = drMod(&quot;TS_Group&quot;)
lblGroup.Text = TSG

tb_TaskOwner.SelectedIndex = tb_TaskOwner.Items.IndexOf(tb_TaskOwner.Items.FindByValue(&quot;TSO&quot;))
tb_TaskGroup.SelectedIndex = tb_TaskOwner.Items.IndexOf(tb_TaskGroup.Items.FindByValue(&quot;TSG&quot;))




I assume that the values you put into the TSO and TSG variables are what you are trying to set the index to. Yes? If so then you need to pass the variables in. What your passing into the findbyvalue is the string &quot;TSO&quot; not the value that the TSO variable contains.


This is what you should have


Dim TSO As String
Dim TSG As String

TSO = drMod(&quot;TS_Customer&quot;)
lblOwner.Text = TSO

TSG = drMod(&quot;TS_Group&quot;)
lblGroup.Text = TSG

tb_TaskOwner.SelectedIndex = tb_TaskOwner.Items.IndexOf(tb_TaskOwner.Items.FindByValue(TSO))
tb_TaskGroup.SelectedIndex = tb_TaskOwner.Items.IndexOf(tb_TaskGroup.Items.FindByValue(TSG))

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top