Answered by:
keep web form drop down list from chagning

Question
-
User1929727834 posted
In a vb.net 2012 web form application, there is a page that is only accessed by the administrators of the application.
There are 2 drop down list options for the user to select and one submit button. Basically one drop donw list is the language that
is to be selected and the kind of letter that is to be generated. The letter is a 'template' generic letter that the administrator changes.
After the administrator hits the submit button, the template letter is changed in the database and one dropdown list shows the language that
was selected. The problem is the kind of letter drop list does not stay and show what was selcted. The kind of letter drop down lists displays
what the intitial drop down list looked like initially.Thus would you show me how to keep is the kind of letter drop down list from changing once the user clicks the submit button?
Thursday, May 16, 2019 8:03 PM
Answers
-
User665608656 posted
Hi dcofomaha,
According to your description,I can't understand your question clearly.
Is user and administrator the same role?
If not, I suggest that you store the value of the letter drop list selected by the current user through the database when the administrator click the submit button, and then when the user logs in, he needs to get the corresponding value in the database and let the letter drop list automatically reverse the value.
If it's the same role, I suggest that you use Session to store the index selected by your letter drop list. By clicking the submit button, you can get the session value and change the selection index of the letter drop list to the index selected before clicking the button (that is, the value stored in the session).
Here is an example used by Session:
<form id="form1" runat="server"> <div> <asp:DropDownList ID="DropDownList1" runat="server" ></asp:DropDownList> <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" /> <br /> <asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList> </div> </form>
code behind:
Class SurroundingClass Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not IsPostBack Then DropDownList1.DataSource = BindData1() DropDownList1.DataTextField = "Language" DropDownList1.DataValueField = "Language" DropDownList1.DataBind() DropDownList2.DataSource = BindData2(DropDownList1.Text.ToString()) DropDownList2.DataTextField = "Letter" DropDownList2.DataValueField = "Letter" DropDownList2.DataBind() Session("selectIndex") = 0 End If End Sub Private Function BindData2(ByVal Language As String) As DataTable Dim connectionString As String = ConfigurationManager.ConnectionStrings("TestConnectionString1").ConnectionString Dim strSql As String = "select Letter from Language where Language =@Language" Dim parameters As SqlParameter() = {New SqlParameter("@Language", Language)} Using connection As SqlConnection = New SqlConnection(connectionString) Using cmd As SqlCommand = New SqlCommand(strSql, connection) For Each parameter As SqlParameter In parameters cmd.Parameters.Add(parameter) Next Try connection.Open() Dim da As SqlDataAdapter = New SqlDataAdapter(cmd) Dim ds As DataSet = New DataSet() da.Fill(ds) Return ds.Tables(0) Catch ex As SqlException Throw New Exception(ex.Message) Finally connection.Close() End Try End Using End Using End Function Private Function BindData1() As DataTable Dim connectionString As String = ConfigurationManager.ConnectionStrings("TestConnectionString1").ConnectionString Dim strSql As String = "select distinct Language from Language" Using connection As SqlConnection = New SqlConnection(connectionString) Using cmd As SqlCommand = New SqlCommand(strSql, connection) Try connection.Open() Dim da As SqlDataAdapter = New SqlDataAdapter(cmd) Dim ds As DataSet = New DataSet() da.Fill(ds) Return ds.Tables(0) Catch ex As SqlException Throw New Exception(ex.Message) Finally connection.Close() End Try End Using End Using End Function Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) DropDownList2.DataSource = BindData2(DropDownList1.SelectedItem.Text.ToString()) DropDownList2.DataBind() DropDownList2.DataValueField = "Letter" DropDownList2.DataTextField = "Letter" DropDownList2.SelectedIndex = Convert.ToInt32(Session("selectIndex")) End Sub Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Session("selectIndex") = DropDownList2.SelectedIndex End Sub End Class
The result of my work demo:
Best Regards,YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 17, 2019 9:34 AM
All replies
-
User2053451246 posted
Show the code that runs when the Submit button is clicked as well as any page load events.
Thursday, May 16, 2019 8:07 PM -
User665608656 posted
Hi dcofomaha,
According to your description,I can't understand your question clearly.
Is user and administrator the same role?
If not, I suggest that you store the value of the letter drop list selected by the current user through the database when the administrator click the submit button, and then when the user logs in, he needs to get the corresponding value in the database and let the letter drop list automatically reverse the value.
If it's the same role, I suggest that you use Session to store the index selected by your letter drop list. By clicking the submit button, you can get the session value and change the selection index of the letter drop list to the index selected before clicking the button (that is, the value stored in the session).
Here is an example used by Session:
<form id="form1" runat="server"> <div> <asp:DropDownList ID="DropDownList1" runat="server" ></asp:DropDownList> <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" /> <br /> <asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList> </div> </form>
code behind:
Class SurroundingClass Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not IsPostBack Then DropDownList1.DataSource = BindData1() DropDownList1.DataTextField = "Language" DropDownList1.DataValueField = "Language" DropDownList1.DataBind() DropDownList2.DataSource = BindData2(DropDownList1.Text.ToString()) DropDownList2.DataTextField = "Letter" DropDownList2.DataValueField = "Letter" DropDownList2.DataBind() Session("selectIndex") = 0 End If End Sub Private Function BindData2(ByVal Language As String) As DataTable Dim connectionString As String = ConfigurationManager.ConnectionStrings("TestConnectionString1").ConnectionString Dim strSql As String = "select Letter from Language where Language =@Language" Dim parameters As SqlParameter() = {New SqlParameter("@Language", Language)} Using connection As SqlConnection = New SqlConnection(connectionString) Using cmd As SqlCommand = New SqlCommand(strSql, connection) For Each parameter As SqlParameter In parameters cmd.Parameters.Add(parameter) Next Try connection.Open() Dim da As SqlDataAdapter = New SqlDataAdapter(cmd) Dim ds As DataSet = New DataSet() da.Fill(ds) Return ds.Tables(0) Catch ex As SqlException Throw New Exception(ex.Message) Finally connection.Close() End Try End Using End Using End Function Private Function BindData1() As DataTable Dim connectionString As String = ConfigurationManager.ConnectionStrings("TestConnectionString1").ConnectionString Dim strSql As String = "select distinct Language from Language" Using connection As SqlConnection = New SqlConnection(connectionString) Using cmd As SqlCommand = New SqlCommand(strSql, connection) Try connection.Open() Dim da As SqlDataAdapter = New SqlDataAdapter(cmd) Dim ds As DataSet = New DataSet() da.Fill(ds) Return ds.Tables(0) Catch ex As SqlException Throw New Exception(ex.Message) Finally connection.Close() End Try End Using End Using End Function Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) DropDownList2.DataSource = BindData2(DropDownList1.SelectedItem.Text.ToString()) DropDownList2.DataBind() DropDownList2.DataValueField = "Letter" DropDownList2.DataTextField = "Letter" DropDownList2.SelectedIndex = Convert.ToInt32(Session("selectIndex")) End Sub Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Session("selectIndex") = DropDownList2.SelectedIndex End Sub End Class
The result of my work demo:
Best Regards,YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 17, 2019 9:34 AM