Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 29th, 2004, 10:58 AM
Registered User
 
Join Date: Jun 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with DataGrid, dependent DDL's

hello,

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's selected value) in the template columns.

Everything works fine with I click the edit button. The values get's filled in the first combo and based on which the second also gets filled.

Now in the 'OnSelectedIndexChanged' 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.

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't tell me to check for ispostback=true or false in this case.

In dire need of help.

Bye...
John.


HTML CODE:
<asp:TemplateColumn SortExpression="Category" HeaderText="Category"><ItemTemplate><%# databinder.eval(container.dataitem, "Category") %></ItemTemplate>
<FooterTemplate><asp:DropDownList id="Add_Cat" Runat="server" AutoPostBack="True"> </asp:DropDownList></FooterTemplate>
<EditItemTemplate><asp:DropDownList id="Edit_Cat" Runat="server" AutoPostBack="True" OnSelectedIndexChanged="load_subcat_onindexchange" ></asp:DropDownList></EditItemTemplate>
</asp:TemplateColumn><asp:TemplateColumn HeaderText="Sub-Category" SortExpression="SubCategory"><ItemTemplate><%# databinder.eval(container.dataitem, "SubCategory") %></ItemTemplate>
<FooterTemplate><asp:DropDownList ID="Add_SubCat" Runat="server" /></FooterTemplate>
<EditItemTemplate><asp:DropDownList ID="Edit_SubCat" Runat="server" DataTextField="CATEGORY" DataValueField="CATEG_SUB" /></EditItemTemplate></asp:TemplateColumn>

VBCODE:

'*****On the page load opening connection and filling a dropdown with some dates.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
OPEN_THE_CONNECTION()
If Not Page.IsPostBack Then
FILL_THE_DROPDOWN()
End If
If Not VIEWSTATE("EditItemIndex") Is Nothing Then
DataGrid1.EditItemIndex = viewstate("EditItemIndex")
binddata()
End If
End Sub

 'Opening the connection

Sub OPEN_THE_CONNECTION()
MYCONSTR = New String("Data source=ABC; INITIAL CATALOG=DEF; USER ID=GHI; PASSWORD=MAN;")
MYCON.ConnectionString = MYCONSTR
MYCON.Open()
End Sub

'Filling the dropdown (first control) on the page with dates.

Sub FILL_THE_DROPDOWN()
Dim MYSQL As New StringBuilder
MYSQL.Append("SELECT DISTINCT PERIOD FROM AUDT_PRODUCT_NEW ORDER BY PERIOD")
mycmd.Connection = MYCON
mycmd.CommandText = MYSQL.ToString
Dim MYREADER As SqlClient.SqlDataReader = mycmd.ExecuteReader
If MYREADER.HasRows = True Then
Do Until MYREADER.Read = False
DropDownList1.Items.Add(MYREADER(0))
Loop
End If
MYREADER.Close()
mycmd.Dispose()
End Sub


'Once the dates are filled in the dropdown clicking on the button next to it to show the grid.

private Sub BtnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOpen.Click
binddata()
End Sub

'Filling the grid with the data.

Sub binddata()
Dim MYSQL As New StringBuilder
MYSQL = MYSQL.Append(" SELECT * from AUDT_PRODUCT_NEW where period='" & DropDownList1.SelectedValue & "'")
MYSQL = MYSQL.Append(" order by Prod_code")
MYBINDCMD.Connection = MYCON
MYBINDCMD.CommandText = MYSQL.ToString
MYBINDADAPTER.SelectCommand = MYBINDCMD
MYBINDADAPTER.Fill(myds, "PRODUCTS")
mydv = myds.Tables("PRODUCTS").DefaultView
mydv.Sort = HTMLHIDDENEXPRESSION.Value
DataGrid1.DataSource = mydv
DataGrid1.DataBind()
MYBINDCMD.Dispose()
End Sub

'Item databound

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
Dim MYDATAROW As Data.DataRowView = CType(e.Item.DataItem,
dataRowView)
Dim drop As DropDownList = CType(e.Item.Cells(3).Controls(1), DropDownList)
drop.Items.Add("True")
drop.Items.Add("False")
Dim MYVAL As String = MYDATAROW("promo")
Dim FINDVAL As ListItem = DROP.Items.FindByText(MYVAL)
If Not FINDVAL Is Nothing Then
FINDVAL.Selected = True
End If
drop = CType(e.Item.Cells(4).Controls(1), DropDownList)
drop.Items.Add("True")
drop.Items.Add("False")
MYVAL = MYDATAROW("Mix")
FINDVAL = DROP.Items.FindByText(MYVAL)
If Not FINDVAL Is Nothing Then
FINDVAL.Selected = True
End If
DROP = CType(e.Item.Cells(10).Controls(1), DropDownList)
MYVAL = MYDATAROW("CATEGORY")
mycmd.Connection = MYCON
mycmd.CommandText = "SELECT * FROM AUDT_PRODCATGRY_MAST_NEW WHERE CATEG_MAIN=0 ORDER BY CATEG_SUB"
myadapter.SelectCommand = mycmd
myadapter.Fill(myds, "CATEGORY")
CATDV = myds.Tables("CATEGORY").DefaultView
DROP.DataSource = CATDV
DROP.DataValueField = "CATEG_MAIN"
DROP.DataTextField = "CATEGORY"
DROP.DataBind()
FINDVAL = DROP.Items.FindByText(MYVAL)
If Not FINDVAL Is Nothing Then
FINDVAL.Selected = True
End If
Dim lstsubcat As DropDownList = CType(e.Item.FindControl("Edit_subcat"), DropDownList)
MYVAL = DataBinder.Eval(e.Item.DataItem, "SUBCATEGORY")
getsubcat(lstsubcat, drop, MYVAL)
ElseIf e.Item.ItemType = ListItemType.Footer Then
Dim MYDATAROW As Data.DataRowView = CType(e.Item.DataItem, DataRowView)
Dim DROP As DropDownList = CType(e.Item.Cells(10).Controls(1), DropDownList)
MYBINDCMD.Connection = MYCON
MYBINDCMD.CommandText = "SELECT * FROM AUDT_PRODCATGRY_MAST_NEW WHERE CATEG_MAIN=0 ORDER BY CATEG_SUB"
myadapter.SelectCommand = MYBINDCMD
myadapter.Fill(myds, "CATEGORY")
CATDV = myds.Tables("CATEGORY").DefaultView
DROP.DataSource = CATDV
DROP.DataTextField = "CATEGORY"
DROP.DataValueField = "CATEG_MAIN"
DROP.DataBind()
MYBINDCMD.Dispose()
End If
End Sub

'The following event getting fired when changing the first combobox's list item.(look HTML code above).

Protected Sub load_subcat_onindexchange(ByVal sender As Object, ByVal e As EventArgs)
Dim lstcat As DropDownList = sender
Dim dbitem As DataGridItem = lstcat.Parent.Parent
Dim lstsubcat As DropDownList = dbitem.FindControl("edit_subcat")
getsubcat(lstsubcat, lstcat, -1)
End Sub

'called from load_subcat_onindexchange (above)

Protected Sub getsubcat(ByVal secondcat As DropDownList, ByVal firstcat As DropDownList, ByVal currentcat As String)
Dim myfirstselection As String = firstcat.SelectedItem.Text
Dim MYSQL As New StringBuilder
MYSQL.Remove(0, MYSQL.Length)
MYSQL.Append(" SELECT * FROM AUDT_PRODCATGRY_MAST_NEW WHERE CATEGORY='" & myfirstselection & "' AND CATEG_MAIN='0'")
mycmd.CommandText = MYSQL.ToString
mycmd.Connection = MYCON
Dim myreader As SqlClient.SqlDataReader
myreader = mycmd.ExecuteReader
If myreader.HasRows Then
myreader.Read()
myfirstselection = myreader("CATEG_SUB")
End If
myreader.Close()
mycmd.Dispose()
Dim mysubcat() As String
If Not IsDBNull(currentcat) Then
MYSQL.Remove(0, MYSQL.Length)
MYSQL.Append(" SELECT * FROM AUDT_PRODCATGRY_MAST_NEW WHERE CATEG_MAIN='" & myfirstselection & "' order by categ_sub")
mycmd.CommandText = MYSQL.ToString
myadapter.SelectCommand = mycmd
myadapter.Fill(myds, "SUB")
secondcat.DataSource = myds.Tables("SUB").DefaultView
secondcat.DataValueField = "Categ_sub"
secondcat.DataTextField = "Category"
secondcat.DataBind()
Dim currentlistitem As ListItem = secondcat.Items.FindByText(currentcat)
If Not currentlistitem Is Nothing Then
currentlistitem.Selected = True
End If
myreader.Close()
mycmd.Dispose()
End If
End Sub


 
Old June 29th, 2004, 12:45 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

This jumped out at me:

If Not VIEWSTATE("EditItemIndex") Is Nothing Then
   DataGrid1.EditItemIndex = viewstate("EditItemIndex")
   binddata()
End If

It appears that this is happening every time the page runs. Why are you saving the EditItemIndex in viewstate? Where are you setting the datagrid's EditItemIndex (apart from in that statement in the Page_Load? I don't see a method that does this. Are you setting the EditItemIndex to that viewstate value at some point and using the code in Page_Load to "refresh" the grid? The datagrid will save EditItemIndex within it automatically once it's been set and rebound (which you normally do within the grid event handler). I suspect that this is causing the problem, because every time the page runs on a postback, the grid is bound, the DDL(s) are getting recreated thus loosing their selected value.

Despite your assurance that it's not an "IsPostback" problem, it kinda-sorta is.
 
Old June 30th, 2004, 02:01 AM
Registered User
 
Join Date: Jun 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I'm saving the viewstate with the edititemindex in the click event of editcommand. The code is here. But this viewstate I've added a day back thinking that it may fix the problem but it doesn't. I think by keeping or removing the viewstate is not making any diffence in my code. For U'r reference I'll show U where I assign the viewstate.

Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles DataGrid1.EditCommand
        If e.CommandName = "Edit" Then
            DataGrid1.EditItemIndex = e.Item.ItemIndex
            VIEWSTATE("EditItemIndex") = e.Item.ItemIndex
            binddata()
        End If
End Sub


I just want to know how can I store the value of the dropdownlist before it fires the event 'OnSelectedIndexChanged'. Immediately when I select a item in the DDL it reloads the page becoz I made it's autopostback=true. Before loading the page I should be able to retain it's value, which I'm not able to. If I remove the autopostback=true the event 'OnSelectedIndexChanged' doesn't fire.

Thanks....
John.

 
Old June 30th, 2004, 07:53 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The point I'm trying to make is that you are binding the datagrid every time the page loads. So you are going to have to manually handle everything regarding the state of the controls inside that grid. I'm still not seeing the reason behind storing and using the viewstate item for EditItemIndex. It seems to me that you are causing yourself more difficulties because it of. The datagrid will maintain its own state. Then the items that are in it (i.e. the dropdownlists when in edit mode) will also maintain their own state so you don't have to worry about manually saving the various values and you can also use the autopostback functionality of the DDL.

I think you should change the code that is checking for that viewstate value, and change it to check for Not IsPostback. Leave your edit command handler as-is, just remove the viewstate assignment.

Also, to clarify one of your comments regarding the "OnSelectedIndexChanged" event handler for the DDL. This event will be fired when the page detects that the index of the DDL has changed. This happens on postback. The postback doesn't necessarily have to be driven by the DDLs AutoPostback attribute. Setting that simply forces the page to post when the DDL item is changed. You could have it set to false, then change the DDL, then hit a button and the DDL.OnSelectedIndexChanged event will fire (along with the button click handler).
 
Old June 30th, 2004, 11:35 AM
Registered User
 
Join Date: Jun 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I've fully changed my code according to you i.e., I've removed all
the viewstate(s). I've also set the autopostback='false'. Now after this you say to change the listitem then click on a button to get the 'selectedindexchanged' & reload the page so as to gather the data and fill the second DDL.

Now my major problem will obviously be to place a button. Where do I place a button when already I've edit & delete buttons placed in the grid columns. Even If I put a button somewhere, do U think it looks good? Is it not exaggarating the work of the user? I agree this may solve the problem as immediately when I loose the focus from the DDL after selecting a value, it doesn't postback.

Now lets continue with the old scenario(just removing all the viewstate(s) and making the autopost=true). As of now I've modified the code and removed all the unnecessary columns to avoid confusion due to bulkiness. Now just have a look at my code U'll realise that
when the page reloads there is no binding of data to the grid. And also check "http://aspnet.4guysfromrolla.com/articles/051904-1.aspx" and "........../051904-21.2.aspx" . Whatever I've done is exactly a copy of this article from 4guysfromrolla.com. Infact, in every article of 4guysfromrolla.com they show a live demo for their article but in this article unfortunately there is no demo and I certainly doubt it's really functional.

Have my code:

<Columns>
<asp:TemplateColumn SortExpression="Prod_code" HeaderText="Product">
<ItemTemplate>
<%# databinder.eval(container.dataitem, "PROD_CODE") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Edit_prod" Runat="server" Enabled="false"
Text='<%# databinder.eval(container.dataitem, "PROD_CODE") %>' Columns="5"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="Add_Prod_code" Runat="server" columns="5"> </asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn SortExpression="Category" HeaderText="Category">
<ItemTemplate>
<%# databinder.eval(container.dataitem, "Category") %>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList id="Add_Cat" Runat="server"></asp:DropDownList>
</FooterTemplate>
<EditItemTemplate>
<asp:DropDownList id="Edit_Cat" Runat="server" AutoPostBack="TRUE" OnSelectedIndexChanged="load_subcat_onindexchange" >
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Sub-Category" SortExpression="SubCategory">
<ItemTemplate>
<%# databinder.eval(container.dataitem, "SubCategory") %>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="Add_SubCat" Runat="server" />
</FooterTemplate>
<EditItemTemplate>
<asp:DropDownList ID="Edit_SubCat" Runat="server" DataTextField="CATEGORY" DataValueField="CATEG_SUB" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update" HeaderText="Edit" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" ButtonType="PushButton" headerText="Delete" CommandName="BtnDel"></asp:ButtonColumn>
</Columns>

Imports System.Text
Imports System.Data.SqlClient
Imports System.Data.SqlTypes

Public Class Testing1
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents HTMLHIDDENEXPRESSION As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents HTMLCAT As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents HTMLSUBCAT As System.Web.UI.HtmlControls.HtmlInputHidden

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Dim MYCON As New SqlClient.SqlConnection
    Dim mycmd As New SqlClient.SqlCommand
    Dim myadapter As New SqlClient.SqlDataAdapter
    Dim MYBINDADAPTER As New SqlClient.SqlDataAdapter
    Dim MYBINDCMD As New SqlClient.SqlCommand
    Dim myds As New Data.DataSet
    Dim mydv As New Data.DataView
    Dim PERIODDV As New Data.DataView
    Dim CATDV As New Data.DataView
    Dim SUBCATDV As New Data.DataView
    Dim MYCONSTR As String, i As Integer

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        OPEN_THE_CONNECTION()
        If Not Page.IsPostBack Then
            binddata()
        End If
    End Sub
    Sub binddata()
        'mysql = mysql.Append("Select Report_seq, Prod_code, Description, Category")
        'mysql = mysql.Append(" from AUDT_PRODUCT_NEW where period='" & DropDownList1.SelectedValue & "'")
        'mysql = mysql.Append(" order by Prod_code")
        Dim MYSQL As New StringBuilder
        MYSQL = MYSQL.Append(" SELECT PROD_CODE, CATEGORY, SUBCATEGORY from AUDT_PRODUCT_NEW where period='CM052004'")
        MYSQL = MYSQL.Append(" order by Prod_code")

        MYBINDCMD.Connection = MYCON
        MYBINDCMD.CommandText = MYSQL.ToString
        MYBINDADAPTER.SelectCommand = MYBINDCMD
        MYBINDADAPTER.Fill(myds, "PRODUCTS")
        mydv = myds.Tables("PRODUCTS").DefaultView
        mydv.Sort = HTMLHIDDENEXPRESSION.Value
        DataGrid1.DataSource = mydv
        DataGrid1.DataBind()
        MYBINDCMD.Dispose()
    End Sub
    Sub OPEN_THE_CONNECTION()
        MYCONSTR = New String("Data source=ABC; INITIAL CATALOG=DEF; USER ID=JOHN; PASSWORD=MAN;")
        MYCON.ConnectionString = MYCONSTR
        MYCON.Open()
    End Sub

    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
        If e.Item.ItemType = ListItemType.EditItem Then
            Dim MYDATAROW As Data.DataRowView = CType(e.Item.DataItem, DataRowView)
            Dim drop As DropDownList = CType(e.Item.Cells(1).Controls(1), DropDownList)
            Dim MYVAL As String = MYDATAROW("CATEGORY")
            mycmd.Connection = MYCON
            mycmd.CommandText = "SELECT * FROM AUDT_PRODCATGRY_MAST_NEW WHERE CATEG_MAIN=0 ORDER BY CATEG_SUB"
            myadapter.SelectCommand = mycmd
            myadapter.Fill(myds, "CATEGORY")
            CATDV = myds.Tables("CATEGORY").DefaultView
            DROP.DataSource = CATDV
            DROP.DataValueField = "CATEG_MAIN"
            DROP.DataTextField = "CATEGORY"
            DROP.DataBind()
            Dim FINDVAL As ListItem = DROP.Items.FindByText(MYVAL)
            If Not FINDVAL Is Nothing Then
                FINDVAL.Selected = True
            End If
            Dim lstsubcat As DropDownList = CType(e.Item.FindControl("Edit_subcat"), DropDownList)
            MYVAL = DataBinder.Eval(e.Item.DataItem, "SUBCATEGORY")
            getsubcat(lstsubcat, drop, MYVAL)

        ElseIf e.Item.ItemType = ListItemType.Footer Then
            Dim MYDATAROW As Data.DataRowView = CType(e.Item.DataItem, DataRowView)
            Dim DROP As DropDownList = CType(e.Item.Cells(1).Controls(1), DropDownList)
            MYBINDCMD.Connection = MYCON
            MYBINDCMD.CommandText = "SELECT * FROM AUDT_PRODCATGRY_MAST_NEW WHERE CATEG_MAIN=0 ORDER BY CATEG_SUB"
            myadapter.SelectCommand = MYBINDCMD
            myadapter.Fill(myds, "CATEGORY")
            CATDV = myds.Tables("CATEGORY").DefaultView
            DROP.DataSource = CATDV
            DROP.DataTextField = "CATEGORY"
            DROP.DataValueField = "CATEG_MAIN"
            DROP.DataBind()
            MYBINDCMD.Dispose()
        End If
    End Sub


    Sub CLOSE_THE_CONNECTION()
        MYCON.Close()
        MYCON.Dispose()
    End Sub

    Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles DataGrid1.EditCommand
        If e.CommandName = "Edit" Then
            DataGrid1.EditItemIndex = e.Item.ItemIndex
            binddata()
        End If
    End Sub

    Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEvent Args) Handles DataGrid1.PageIndexChanged
        DataGrid1.CurrentPageIndex = e.NewPageIndex
        binddata()
    End Sub

    Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEvent Args) Handles DataGrid1.SortCommand
        HTMLHIDDENEXPRESSION.Value = e.SortExpression
        binddata()
    End Sub

    Private Sub DataGrid1_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles DataGrid1.CancelCommand
        DataGrid1.EditItemIndex = -1
        binddata()
    End Sub


    Protected Sub getsubcat(ByVal secondcat As DropDownList, ByVal firstcat As DropDownList, ByVal currentcat As String)
        Dim myfirstselection As String = firstcat.SelectedItem.Text
        Dim MYSQL As New StringBuilder
        MYSQL.Remove(0, MYSQL.Length)
        MYSQL.Append(" SELECT * FROM AUDT_PRODCATGRY_MAST_NEW WHERE CATEGORY='" & myfirstselection & "' AND CATEG_MAIN='0'")
        mycmd.CommandText = MYSQL.ToString
        mycmd.Connection = MYCON
        Dim myreader As SqlClient.SqlDataReader
        myreader = mycmd.ExecuteReader
        If myreader.HasRows Then
            myreader.Read()
            myfirstselection = myreader("CATEG_SUB")
        End If
        myreader.Close()
        mycmd.Dispose()

        Dim mysubcat() As String
        If Not IsDBNull(currentcat) Then
            MYSQL.Remove(0, MYSQL.Length)
            MYSQL.Append(" SELECT * FROM AUDT_PRODCATGRY_MAST_NEW WHERE CATEG_MAIN='" & myfirstselection & "' order by categ_sub")
            mycmd.CommandText = MYSQL.ToString
            myadapter.SelectCommand = mycmd
            myadapter.Fill(myds, "SUB")
            secondcat.DataSource = myds.Tables("SUB").DefaultView
            secondcat.DataValueField = "Categ_sub"
            secondcat.DataTextField = "Category"
            secondcat.DataBind()
            Dim currentlistitem As ListItem = secondcat.Items.FindByText(currentcat)
            If Not currentlistitem Is Nothing Then
                currentlistitem.Selected = True
            End If
            myreader.Close()
            mycmd.Dispose()
        End If
    End Sub


    Protected Sub load_subcat_onindexchange(ByVal sender As Object, ByVal e As EventArgs)
        Dim lstcat As DropDownList = sender
        Response.Write(lstcat.SelectedItem.Text)
        Dim dbitem As DataGridItem = lstcat.Parent.Parent
        Dim lstsubcat As DropDownList = dbitem.FindControl("edit_subcat")
        getsubcat(lstsubcat, lstcat, -1)
    End Sub
End Class



I really would appreciate for your further comments and suggestions...

Regards...
John.


 
Old June 30th, 2004, 12:01 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

John,
The point of my clarification wasn't to say that you should use the technique of an additional button, it was just to explain that saying "without autopostback the event doesn't fire" is somewhat misleading. The event will fire when the page is posted, but the control is just not causing a postback automatically.

Ok, so with this new code, what happens?
 
Old June 30th, 2004, 11:15 PM
Registered User
 
Join Date: Jun 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I've the same problem that it reads the first item in the first DDL where infact the item selected could be anything other than the first. I also don't bind anywhere. So, now always my second DDL is getting filled based on the selectedvalue in the first DDL i.e. the first item in the first DDL. I think it's not gonna work. My code as I said, is what I tried from one of the artciles from the most famous 'http://4guysfromrolla.com' asp pages. I doubt whether it's truly functional.

Thanks....
John.






Similar Threads
Thread Thread Starter Forum Replies Last Post
form submit problem/dependent list menus alexscan Pro PHP 0 January 1st, 2008 06:29 PM
Dependent DropDown In DataGrid software_developer_kk ASP.NET 1.0 and 1.1 Professional 3 January 29th, 2007 03:13 AM
dependent drop downs p2pMember ASP.NET 1.0 and 1.1 Professional 0 July 19th, 2006 05:25 AM
dependent select menu hastikeyvan Dreamweaver (all versions) 1 February 27th, 2006 04:34 AM
Look up value dependent on 2 other variables nkrukovsky Excel VBA 1 September 22nd, 2004 09:51 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.