in Search
     
Latest post 04-01-2008 9:01 AM by ninjai. 12 replies.
Page 1 of 1 (13 items)
Sort Posts: Previous Next
  • 03-18-2008 9:57 PM

    accessing a dynamically added dropdownlist's selecteditems value/text

    mga kuya help po ulit pls...

    gagawa po ako ng table ng mga employee sa CODE BEHIND... yun kasi ang approach na pinag aaralan ko ngayun. plus ayaw ko tlga masanay ng nag dadrag n drop ng mga control. As much as possible I am also avoiding writing anything on the HTML source because that will spoil my CODE-BEHIND training.  so please don't reply suggesting a different approach, I just want to know whats wrong.

    galing po kasi ako sa CLASSIC ASP and 2 months p lang ako nag aaral ng asp.net... I don't know kung maniniwala kayo saken pero tlgang hindi masyadong nkakatulong yung experience ko sa Classic. Maybe because lagi kong hinahanap yung substitute ng How to's ng .NET over the Classic.

    anyway the my real problem as of this moment is I cant get the selectedItem.Value of a server-side Dropdownlist...

    eto po ang ACTUAL code..

    <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="FM_Employee_Master.aspx.vb" Inherits="FM_Employee_Master" title="Untitled Page" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <div class="date_break">&nbsp;&nbsp;File Maintenance - Employee Personal Information</div>

    <div class="post" runat="server" id="Div_GridHolder" style="margin-left:auto; margin-right:auto">

    <h1 style='text-align: left;' runat="server" id="H1_Title1"></h1>

    <span class='post_body' style="width:100%">

    <asp:Table ID="Emp_Grid" runat="server" Width="100%"></asp:Table>

    </span>

    </div>

    </asp:Content>

     

     

    Partial Class FM_Employee_Master

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not Page.IsPostBack Then

    Dim HeaderROW As TableRow = New TableRow

    Dim HeaderCELL_ID As TableCell = New TableCell

    Dim HeaderCELL_Name As TableCell = New TableCell

    Dim HeaderCELL_Gender As TableCell = New TableCell

    Dim HeaderCELL_Bday As TableCell = New TableCell

    Dim HeaderCELL_EmpStat As TableCell = New TableCell

    Dim HeaderCELL_BLANK As TableCell = New TableCell

    Dim myConn As myConnections = New myConnections

    Dim mySQL As String

    Div_GridHolder.Style("width") = "90%"

    With H1_Title1

    .InnerText = "Employee List"

    .Style("text-align") = "Center"

    .Style("border") = 1

    End With

    With HeaderROW

    .Cells.Add(HeaderCELL_ID)

    .Cells.Add(HeaderCELL_Name)

    .Cells.Add(HeaderCELL_Gender)

    .Cells.Add(HeaderCELL_Bday)

    .Cells.Add(HeaderCELL_EmpStat)

    .Cells.Add(HeaderCELL_BLANK)

    End With

    With HeaderCELL_ID

    .BorderWidth = 1

    .BorderColor = Drawing.Color.Gray

    .Style("text-align") = "center"

    .Style("font-weight") = "bold"

    .Style("text-decoration") = "Underline"

    .BackColor = Drawing.Color.DarkSlateGray

    .Text = "I.D."

    End With

    With HeaderCELL_Name

    .BorderWidth = 1

    .BorderColor = Drawing.Color.Gray

    .Style("text-align") = "center"

    .Style("font-weight") = "bold"

    .Style("text-decoration") = "Underline"

    .BackColor = Drawing.Color.DarkSlateGray

    .Text = "Name"

    End With

    With HeaderCELL_Gender

    .BorderWidth = 1

    .BorderColor = Drawing.Color.Gray

    .Style("text-align") = "center"

    .Style("font-weight") = "bold"

    .Style("text-decoration") = "Underline"

    .BackColor = Drawing.Color.DarkSlateGray

    .Text = "Gender"

    End With

    With HeaderCELL_Bday

    .BorderWidth = 1

    .BorderColor = Drawing.Color.Gray

    .Style("text-align") = "center"

    .Style("font-weight") = "bold"

    .Style("text-decoration") = "Underline"

    .BackColor = Drawing.Color.DarkSlateGray

    .Text = "Date of Birth"

    End With

    With HeaderCELL_EmpStat

    .BorderWidth = 1

    .BorderColor = Drawing.Color.Gray

    .Style("text-align") = "center"

    .Style("font-weight") = "bold"

    .Style("text-decoration") = "Underline"

    .BackColor = Drawing.Color.DarkSlateGray

    .Text = "Employment Status"

    End With

    With HeaderCELL_BLANK

    .BorderWidth = 1

    .BorderColor = Drawing.Color.Gray

    .BackColor = Drawing.Color.DarkSlateGray

    End With

    With Emp_Grid

    .Rows.Add(HeaderROW)

    .CellSpacing = 0

    .CellPadding = 0

    myConn.Open()

    mySQL = "Select * from [Personal_Info]"

    Dim myCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(mySQL, myConn.Connection_Output)

    Dim myDataReader As System.Data.SqlClient.SqlDataReader = myCommand.ExecuteReader()

    While myDataReader.Read()
    Dim Body_Row As New TableRow

    .Rows.Add(Body_Row)

    Dim Body_Cell_ID As New TableCell

    Dim Body_Cell_Name As New TableCell

    Dim Body_Cell_Gender As New TableCell

    Dim Body_Cell_BDay As New TableCell

    Dim Body_Cell_Emp_Stat As New TableCell

    Dim Body_Cell_Action As New TableCell

    Body_Row.Cells.Add(Body_Cell_ID)

    Body_Row.Cells.Add(Body_Cell_Name)

    Body_Row.Cells.Add(Body_Cell_Gender)

    Body_Row.Cells.Add(Body_Cell_BDay)

    Body_Row.Cells.Add(Body_Cell_Emp_Stat)

    Body_Row.Cells.Add(Body_Cell_Action)

    With Body_Cell_ID

    .BorderWidth = 1

    .BorderColor = Drawing.Color.Gray

    .Style("text-align") = "center"

    .Text = myDataReader("Emp_ID")

    End With

    With Body_Cell_Name

    .BorderWidth = 1

    .BorderColor = Drawing.Color.Gray

    .Style("text-align") = "center"

    Body_Cell_Name.Text = myDataReader("Name_Last") & ", " & myDataReader("Name_First") & " " & myDataReader("Name_Middle")

    End With

    With Body_Cell_Gender

    .BorderWidth = 1

    .BorderColor = Drawing.Color.Gray

    .Style("text-align") = "center"

    Dim strGender As String

    If myDataReader("Gender") Then

    strGender = "Male"

    Else

    strGender = "Female"

    End If

    Body_Cell_Gender.Text = strGender

    End With

    With Body_Cell_BDay

    .BorderWidth = 1

    .BorderColor = Drawing.Color.Gray

    .Style("text-align") = "center"

    .Text = FormatDateTime(myDataReader("Birthdate"), DateFormat.LongDate)

    End With

    With Body_Cell_Emp_Stat

    .BorderWidth = 1

    .BorderColor = Drawing.Color.Gray

    .Style("text-align") = "center"

    Dim myConn2 As myConnections = New myConnections

    Dim mySQL2 As String = "Select employment_Status.title from employment_Status employment_Status, Current_Employment_Stat Current_Employment_Stat WHERE (Current_Employment_Stat.employmentstatus_ID = employment_Status.employmentstatus_ID) AND Current_Employment_Stat.isCurrent = 1 AND Current_Employment_Stat.Emp_ID = @Emp_Id"

    myConn2.Open()

    Dim myCommand2 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(mySQL2, myConn2.Connection_Output)

    myCommand2.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Emp_Id", myDataReader("Emp_ID")))

    Dim myDataReader2 As System.Data.SqlClient.SqlDataReader = myCommand2.ExecuteReader()While myDataReader2.Read()
    .Text = myDataReader2("title")

    End While

    myConn2.Close()

    End With

    With Body_Cell_Action

    Dim i As Integer = myDataReader("Emp_ID")

    Dim DrpLst_Action(i) As System.Web.UI.WebControls.DropDownList

    DrpLst_Action(i) = New System.Web.UI.WebControls.DropDownList

    With DrpLst_Action(i)

    .ID = "ID_" & i

    .Attributes.Add("runat", "server")

    .Items.Add(New ListItem("--Select--", 0))

    .Items.Add(New ListItem("View/Edit personal info", 1))

    .Items(1).Value = 1

    .Items.Add(
    New ListItem("View/Edit employment record", 2))

    .Items.Add(New ListItem("View time-in/time-out logs", 3))

    .Attributes.Add("onchange", "window.location.href='FM_Employee_Master.aspx?actionSelected=" & DrpLst_Action(i).SelectedItem.Value & "'") '<-------THis is the PROBLEM AREA

    End With

    .BorderWidth = 1

    .BorderColor = Drawing.Color.Gray

    .Style("text-align") = "center"

    .Controls.Add(DrpLst_Action(i))

    End With

    End While

    myConn.Close()

    End With

    End If

    End Sub

    End Class

     

     

    wala pong problem sa pag generate ng table at pag retrieve ng value sa dbase. ang problema tlga e yung dropdownlist value.... nde ko makuha..

    onchange ng dropdownlist I tried to call the page again and pass the value of the dropdownlist sa querystring kaya lang laging yung unang item lang ang nakukuha nya.

    salamat po

    • Post Points: 20
  • 03-18-2008 10:57 PM In reply to

    • jop
    • Top 25 Contributor
    • Joined on 08-08-2006
    • Singapore
    • Posts 187
    • Points 3,185

    Re: accessing a dynamically added dropdownlist's selecteditems value/text

    Ok, first off... [[snipped out because you don't want comments about a different approach i.e. postback]]

    About the onchange handler, use an alert message first to see if you've wired up the event properly.

    Attributes.Add("onchange", "alert(onchange triggered)")

    Once you're sure the event is getting triggered, replace the text to display it with the value that you want to get from the drop down list.

    [jop]

    • Post Points: 20
  • 03-18-2008 11:02 PM In reply to

    Re: accessing a dynamically added dropdownlist's selecteditems value/text

    You have to set its DataValueField property

    YourDropDownList.DataTextField=FieldFromDBtoDisplay; eg "Category"
    YourDropDownList.DataValueField=FieldFromDB; eg "CategoryID"

    • Post Points: 20
  • 03-18-2008 11:31 PM In reply to

    Re: accessing a dynamically added dropdownlist's selecteditems value/text

    kuya jop..... gumagana po yung alert

     

     

    kuya ulysses.... im not trying to populate the dropdownlist i just want to trigger the onchange then get its value then maybe pass it on the querystring

    • Post Points: 5
  • 03-18-2008 11:35 PM In reply to

    Re: accessing a dynamically added dropdownlist's selecteditems value/text

    DrpLst_Action(i).Attributes.Add("onchange", "alert('hi')")  <--------------working fine

    DrpLst_Action(i).Attributes.Add("onchange", "alert('" & DrpLst_Action(i).SelectedItem.Value & "')") <------alert always shows  "0" kahit anung piliin sa dropdownlist.

    • Post Points: 35
  • 03-19-2008 12:47 AM In reply to

    • jop
    • Top 25 Contributor
    • Joined on 08-08-2006
    • Singapore
    • Posts 187
    • Points 3,185

    Re: accessing a dynamically added dropdownlist's selecteditems value/text

    SuperNoob:

    DrpLst_Action(i).Attributes.Add("onchange", "alert('" & DrpLst_Action(i).SelectedItem.Value & "')") <------alert always shows  "0" kahit anung piliin sa dropdownlist.

    Yang code na yan, tumatakbo sa server side. pag nag-execute yan, "alert(0)" and nagiging result at yun ang na-attach sa onchange.

    Kailangang ma-evaluate yung value nung drop down sa client side so you have to get the value using javascript... document.getElementByName(the control's client id).value or something.

    Pero hint lang ha: alam kong gusto mong matutunan kung paano gumagana yan, pero hindi yan yung typical ASP.NET programmer way ng pag-kuha ng value ng dropdownlist immediately. Normally set mo lang yung AutoPostback=true tapos kunin mo na lang yung SelectedItem sa postback handler.

    [jop]

    • Post Points: 20
  • 03-19-2008 1:39 AM In reply to

    Re: accessing a dynamically added dropdownlist's selecteditems value/text

    using my code plus adding a ''DrpLst_Action(i).autopostback = true" results to: a postback that does not generate my employee table everytime I selectchange anyone of my dropdownlist

    • Post Points: 35
  • 03-19-2008 2:13 AM In reply to

    • n.ocampo
    • Top 50 Contributor
    • Joined on 09-22-2005
    • Makati
    • Posts 40
    • Points 1,145

    Re: accessing a dynamically added dropdownlist's selecteditems value/text

    hi SuperNoob, just a suggestion you can use GridView to display your data in tabular format. In ASP.NET you don't need to get your hands dirty to achieve this. 

    • Post Points: 20
  • 03-19-2008 3:04 AM In reply to

    • jop
    • Top 25 Contributor
    • Joined on 08-08-2006
    • Singapore
    • Posts 187
    • Points 3,185

    Re: accessing a dynamically added dropdownlist's selecteditems value/text

    SuperNoob:

    using my code plus adding a ''DrpLst_Action(i).autopostback = true" results to: a postback that does not generate my employee table everytime I selectchange anyone of my dropdownlist

    That's easy.

    Yun kasing code na nag-generate ng employee tables mo nasa loob ng If Not Postback block dun sa Page_Load. Of course, di nga madadaanan yun during postback.

    Maganda ring basahin mo yung asp.net page lifecycle para maintindiha mo kung bakit nangyayari yun: http://msdn2.microsoft.com/en-us/library/ms178472.aspx

    [jop]

    • Post Points: 5
  • 03-19-2008 3:13 AM In reply to

    • jop
    • Top 25 Contributor
    • Joined on 08-08-2006
    • Singapore
    • Posts 187
    • Points 3,185

    Re: accessing a dynamically added dropdownlist's selecteditems value/text

    n.ocampo:

    hi SuperNoob, just a suggestion you can use GridView to display your data in tabular format. In ASP.NET you don't need to get your hands dirty to achieve this. 

    +1

    In addition to the page lifecycle reading, also have a look at the asp.net quickstarts and look at the GridView samples: http://quickstarts.asp.net/QuickstartV20/aspnet/

    [jop]

    • Post Points: 5
  • 03-20-2008 8:05 AM In reply to

    • ninjai
    • Top 100 Contributor
    • Joined on 03-19-2008
    • Posts 12
    • Points 240

    Re: accessing a dynamically added dropdownlist's selecteditems value/text

    Answer

    SuperNoob:

    DrpLst_Action(i).Attributes.Add("onchange", "alert('hi')")  <--------------working fine

    DrpLst_Action(i).Attributes.Add("onchange", "alert('" & DrpLst_Action(i).SelectedItem.Value & "')") <------alert always shows  "0" kahit anung piliin sa dropdownlist.

     

     

    Hi SuperNoob, 

    Just my 2 cents. On the second ..Attributes.Add, you're concatenating the selected value of your DDLB into a Javascript call. Since the above codes are executed from the server, it will always return zero . Remember, you created the DDLB on the fly and you added some items on the fly as well.

    What you need to do is to get the value from the client side. I think you have to do something like... 

    DrpLst_Action(i).Attributes.Add("onchange", "alert(this.value)")

    this will get the current selected value of the DDLB from the client side.

    Regards.

    • Post Points: 20
  • 03-23-2008 7:08 PM In reply to

    Re: accessing a dynamically added dropdownlist's selecteditems value/text

    wahahahaha ang galing mo kuya ninjai isa kang henyo......

    hyyy kala ko mag gi-giveup na ko sa approach  na 'to.

     "this.value" <-- magic word

     

    by setting the DrpLst_Action(i).autopostback = false

    and adding the magic word: DrpLst_Action(i).Attributes.Add("onchange", "window.location.href=""FM_Employee_Master.aspx?SelectedValue=""+this.value")

     

    wahahaha ang galing tlga madami na akong magagawa dito

    kuya ninjai salamat ha

    maraming salamat din po sa ibang nag post... sana magkaron din ng chance na ako naman ang mkatulong sa inyu. thank you po...

    • Post Points: 20
  • 04-01-2008 9:01 AM In reply to

    • ninjai
    • Top 100 Contributor
    • Joined on 03-19-2008
    • Posts 12
    • Points 240

    Re: accessing a dynamically added dropdownlist's selecteditems value/text

     you're welcome :)

    • Post Points: 5
Page 1 of 1 (13 items)