4guysfromrolla article doesn't work

Results 1 to 2 of 2

Thread: 4guysfromrolla article doesn't work

  1. #1
    Join Date
    Dec 1969

    Default 4guysfromrolla article doesn't work

    The code was copied exactly from one of the articles of 4guysfromrolla.com "http://aspnet.4guysfromrolla.com/articles/051904-1.aspx". <BR><BR>The only change is, I&#039;ve on the page load a DDL, which gets filled with dates, and when I select a date, basing on this the datagrid gets filled. This DDL doesn&#039;t have anything todo with my two dependent DDL&#039;s in the datagrid. The actual problem is inside the datagrid with two DDL&#039;s. <BR><BR>hello, <BR><BR>Can anyone help me get out of this problem of more than a week. I wanted to use dependent dropdownlist (two dropdown boxes the second one fetching the data based on the first&#039;s selected value) in the template columns. <BR><BR>Everything works fine with I click the edit button. The values get&#039;s filled in the first combo and based on which the second also gets filled. <BR><BR>Now in the &#039;OnSelectedIndexChanged&#039; event of the first combo I call a routine to fill the second combo. I also have made the autopostback="true" for the first combo. When I select a different value in the first combo my second combo should get filled by reloading the page. Here also everything works fine but the major problem comes when it makes the first item in the first combo selected and not the one which I selected. I mean when page gets posted it always takes the first item from the first combo or it takes the previously selected item from the first combo and not the current one at the time of posting. This creates a problem because the second combo is getting filled based on the first value of my first combo. How do I stop this. How can I store the value of the changed item from the first combo when it page gets loaded. <BR><BR>One more thing keep it in mind before coming to a conclusion that the first time I load the page only with a single combo showing me different dates. Once I select a date and press a button next to it my grid gets filled. So U can&#039;t tell me to check for ispostback=true or false in this case. <BR><BR>In dire need of help. <BR><BR>Bye... <BR>John. <BR><BR><BR>HTML CODE: <BR><BR>&#060;asp:TemplateColumn SortExpression="Category" HeaderText="Category"&#062;&#060;ItemTemplate&#062 ;&#060;%# databinder.eval(container.dataitem, "Category") %&#062;&#060;/ItemTemplate&#062; <BR>&#060;FooterTemplate&#062;&#060;asp:DropDownLi st id="Add_Cat" Runat="server" AutoPostBack="True"&#062; &#060;/asp:DropDownList&#062;&#060;/FooterTemplate&#062; <BR>&#060;EditItemTemplate&#062;&#060;asp:DropDown List id="Edit_Cat" Runat="server" AutoPostBack="True" OnSelectedIndexChanged="load_subcat_onindexchange" &#062;&#060;/asp:DropDownList&#062;&#060;/EditItemTemplate&#062; <BR>&#060;/asp:TemplateColumn&#062;&#060;asp:TemplateColumn HeaderText="Sub-Category" SortExpression="SubCategory"&#062;&#060;ItemTempla te&#062;&#060;%# databinder.eval(container.dataitem, "SubCategory") %&#062;&#060;/ItemTemplate&#062; <BR>&#060;FooterTemplate&#062;&#060;asp:DropDownLi st ID="Add_SubCat" Runat="server" /&#062;&#060;/FooterTemplate&#062; <BR>&#060;EditItemTemplate&#062;&#060;asp:DropDown List ID="Edit_SubCat" Runat="server" DataTextField="CATEGORY" DataValueField="CATEG_SUB" /&#062;&#060;/EditItemTemplate&#062;&#060;/asp:TemplateColumn&#062; <BR><BR>VBCODE: <BR>&#039;*****On the page load opening connection and filling a dropdown with some dates. <BR><BR>Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load <BR>&#039;Put user code to initialize the page here <BR><BR>OPEN_THE_CONNECTION() <BR>If Not Page.IsPostBack Then <BR>FILL_THE_DROPDOWN() <BR>End If <BR>If Not VIEWSTATE("EditItemIndex") Is Nothing Then <BR>DataGrid1.EditItemIndex = viewstate("EditItemIndex") <BR>binddata() <BR>End If <BR>End Sub <BR><BR>&#039;Opening the connection <BR>Sub OPEN_THE_CONNECTION() <BR>MYCONSTR = New String("Data source=ABC; INITIAL CATALOG=DEF; USER ID=GHI; PASSWORD=MAN;") <BR>MYCON.ConnectionString = MYCONSTR <BR>MYCON.Open() <BR>End Sub <BR><BR>&#039;Filling the dropdown (first control) on the page with dates. <BR>Sub FILL_THE_DROPDOWN() <BR>Dim MYSQL As New StringBuilder <BR>MYSQL.Append("SELECT DISTINCT PERIOD FROM AUDT_PRODUCT_NEW ORDER BY PERIOD") <BR>mycmd.Connection = MYCON <BR>mycmd.CommandText = MYSQL.ToString <BR>Dim MYREADER As SqlClient.SqlDataReader = mycmd.ExecuteReader <BR>If MYREADER.HasRows = True Then <BR>Do Until MYREADER.Read = False <BR>DropDownList1.Items.Add(MYREADER(0)) <BR>Loop <BR>End If <BR>MYREADER.Close() <BR>mycmd.Dispose() <BR>End Sub <BR><BR>&#039;Once the dates are filled in the dropdown clicking on the button next to it to show the grid. <BR><BR>Private Sub BtnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOpen.Click <BR>binddata() <BR>End Sub <BR><BR>&#039;Filling the grid with the data. <BR>Sub binddata() <BR>Dim MYSQL As New StringBuilder <BR>MYSQL = MYSQL.Append(" SELECT * from AUDT_PRODUCT_NEW where period=&#039;" & DropDownList1.SelectedValue & "&#039;") <BR>MYSQL = MYSQL.Append(" order by Prod_code") <BR>MYBINDCMD.Connection = MYCON <BR>MYBINDCMD.CommandText = MYSQL.ToString <BR>MYBINDADAPTER.SelectCommand = MYBINDCMD <BR>MYBINDADAPTER.Fill(myds, "PRODUCTS") <BR>mydv = myds.Tables("PRODUCTS").DefaultView <BR>mydv.Sort = HTMLHIDDENEXPRESSION.Value <BR>DataGrid1.DataSource = mydv <BR>DataGrid1.DataBind() <BR>MYBINDCMD.Dispose() <BR>End Sub <BR><BR>&#039;Item databound <BR>Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound <BR>If e.Item.ItemType = ListItemType.EditItem Then <BR>Dim MYDATAROW As Data.DataRowView = CType(e.Item.DataItem, DataRowView) <BR>Dim drop As DropDownList = CType(e.Item.Cells(3).Controls(1), DropDownList) <BR>drop.Items.Add("True") <BR>drop.Items.Add("False") <BR>Dim MYVAL As String = MYDATAROW("promo") <BR>Dim FINDVAL As ListItem = DROP.Items.FindByText(MYVAL) <BR>If Not FINDVAL Is Nothing Then <BR>FINDVAL.Selected = True <BR>End If <BR>drop = CType(e.Item.Cells(4).Controls(1), DropDownList) <BR>drop.Items.Add("True") <BR>drop.Items.Add("False") <BR>MYVAL = MYDATAROW("Mix") <BR>FINDVAL = DROP.Items.FindByText(MYVAL) <BR>If Not FINDVAL Is Nothing Then <BR>FINDVAL.Selected = True <BR>End If <BR>DROP = CType(e.Item.Cells(10).Controls(1), DropDownList) <BR>MYVAL = MYDATAROW("CATEGORY") <BR>mycmd.Connection = MYCON <BR>mycmd.CommandText = "SELECT * FROM AUDT_PRODCATGRY_MAST_NEW WHERE CATEG_MAIN=0 ORDER BY CATEG_SUB" <BR>myadapter.SelectCommand = mycmd <BR>myadapter.Fill(myds, "CATEGORY") <BR>CATDV = myds.Tables("CATEGORY").DefaultView <BR>DROP.DataSource = CATDV <BR>DROP.DataValueField = "CATEG_MAIN" <BR>DROP.DataTextField = "CATEGORY" <BR>DROP.DataBind() <BR>FINDVAL = DROP.Items.FindByText(MYVAL) <BR>If Not FINDVAL Is Nothing Then <BR>FINDVAL.Selected = True <BR>End If <BR>Dim lstsubcat As DropDownList = CType(e.Item.FindControl("Edit_subcat"), DropDownList) <BR>MYVAL = DataBinder.Eval(e.Item.DataItem, "SUBCATEGORY") <BR>getsubcat(lstsubcat, drop, MYVAL) <BR>ElseIf e.Item.ItemType = ListItemType.Footer Then <BR>Dim MYDATAROW As Data.DataRowView = CType(e.Item.DataItem, DataRowView) <BR>Dim DROP As DropDownList = CType(e.Item.Cells(10).Controls(1), DropDownList) <BR>MYBINDCMD.Connection = MYCON <BR>MYBINDCMD.CommandText = "SELECT * FROM AUDT_PRODCATGRY_MAST_NEW WHERE CATEG_MAIN=0 ORDER BY CATEG_SUB" <BR>myadapter.SelectCommand = MYBINDCMD <BR>myadapter.Fill(myds, "CATEGORY") <BR>CATDV = myds.Tables("CATEGORY").DefaultView <BR>DROP.DataSource = CATDV <BR>DROP.DataTextField = "CATEGORY" <BR>DROP.DataValueField = "CATEG_MAIN" <BR>DROP.DataBind() <BR>MYBINDCMD.Dispose() <BR>End If <BR>End Sub <BR><BR>&#039;The following event getting fired when changing the first combobox&#039;s list item.(look HTML code above). <BR><BR>Protected Sub load_subcat_onindexchange(ByVal sender As Object, ByVal e As EventArgs) <BR>Dim lstcat As DropDownList = sender <BR>Dim dbitem As DataGridItem = lstcat.Parent.Parent <BR>Dim lstsubcat As DropDownList = dbitem.FindControl("edit_subcat") <BR>getsubcat(lstsubcat, lstcat, -1) <BR>End Sub <BR><BR>&#039;called from load_subcat_onindexchange (above) <BR>Protected Sub getsubcat(ByVal secondcat As DropDownList, ByVal firstcat As DropDownList, ByVal currentcat As String) <BR>Dim myfirstselection As String = firstcat.SelectedItem.Text <BR>Dim MYSQL As New StringBuilder <BR>MYSQL.Remove(0, MYSQL.Length) <BR>MYSQL.Append(" SELECT * FROM AUDT_PRODCATGRY_MAST_NEW WHERE CATEGORY=&#039;" & myfirstselection & "&#039; AND CATEG_MAIN=&#039;0&#039;") <BR>mycmd.CommandText = MYSQL.ToString <BR>mycmd.Connection = MYCON <BR>Dim myreader As SqlClient.SqlDataReader <BR>myreader = mycmd.ExecuteReader <BR>If myreader.HasRows Then <BR>myreader.Read() <BR>myfirstselection = myreader("CATEG_SUB") <BR>End If <BR>myreader.Close() <BR>mycmd.Dispose() <BR>Dim mysubcat() As String <BR>If Not IsDBNull(currentcat) Then <BR>MYSQL.Remove(0, MYSQL.Length) <BR>MYSQL.Append(" SELECT * FROM AUDT_PRODCATGRY_MAST_NEW WHERE CATEG_MAIN=&#039;" & myfirstselection & "&#039; order by categ_sub") <BR>mycmd.CommandText = MYSQL.ToString <BR>myadapter.SelectCommand = mycmd <BR>myadapter.Fill(myds, "SUB") <BR>secondcat.DataSource = myds.Tables("SUB").DefaultView <BR>secondcat.DataValueField = "Categ_sub" <BR>secondcat.DataTextField = "Category" <BR>secondcat.DataBind() <BR>Dim currentlistitem As ListItem = secondcat.Items.FindByText(currentcat) <BR>If Not currentlistitem Is Nothing Then <BR>currentlistitem.Selected = True <BR>End If <BR>myreader.Close() <BR>mycmd.Dispose() <BR>End If <BR>End Sub <BR>

  2. #2
    Join Date
    Dec 1969

    Default RE: 4guysfromrolla article doesn't work

    Possibly you could check what the value of the list control is by calling Request.Form("[control name]") instead of referencing the control directly (that is, control.Value)<BR><BR>Does that help?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts