单击事件按钮后,下拉列表选择的值消失 [英] Dropdownlist selected value disappears after click event Button

查看:80
本文介绍了单击事件按钮后,下拉列表选择的值消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下下拉列表和搜索按钮

<asp:DropDownList ID="CustomerTypeDropList" runat="server"; 
                    

 <asp:Button ID="SearchBtn" runat="server" Text="  Find  " CssClass="myButton" />


这是我在VB.NET中隐藏的代码

  Dim 容器 As   myContainer
  GridViewDisplay( ByVal  strSql  As  字符串)
  尝试
   dropDownStr = " 
   textfield = " 
   valuefield = " 
   container.dropDownListBind(CustDropList,dropDownStr,textfield,valuefield)
   捕获,例如 As 异常
      resultLbl.Text = ex.Message
  结束 尝试
 结束  


我的数据绑定子位于单独的以下类中

 公共  class  Mycontainer

 Sub  dropDownListBind( ByVal  myDropDown  As  DropDownList,  ByVal  SQLstring  As   String  ByVal 文本字段 As  字符串 As  " ).ConnectionString
        myCommand.Connection = myConnect
        如果 myConnect.State = ConnectionState.已关闭然后
            myConnect.ConnectionString = myConnectionString
            myConnect.Open()
        结束 如果

        myCommand.CommandText = SQLstring
        '  myConnect = ConnectToDatabase(SQLstring)
        myDropDown.DataTextField =文本字段
        myDropDown.DataValueField = valuefield
               
        myreader = myCommand.ExecuteReader()
        myDropDown.DataSource = myreader
        myDropDown.DataBind()
         myreader.Close()
        myConnect.Close()
    结束 
结束  



事件单击必须处于回发状态,因为用户必须在首次加载页面后选择客户的类型.
我的错误是,当用户选择客户类型并单击搜索按钮时,下拉列表中的选定值消失了(下拉列表viewstate不起作用)

解决方案

绑定到数据源时,选择将丢失.您应该在数据层中执行此操作,但是至少要使用if(!IsPostback)(我不知道如何在VB中执行不"操作,只是单词吗?)来检查您是否在回发,如果您愿意,则不要不必要地重新加载数据.


是否已将( IsPostBack )放入了Page_load中,如果没有,则将其放入然后填充dropdownlist


是的,我将此条件放在了以下搜索Button Sub中,但错误不是page_load事件.

受保护的子SearchBtn_Click(ByVal发送者作为对象,ByVal e作为EventArgs)处理SearchBtn.Click

如果不是IsPsotBack,则
GridViewDisplay(strSql)

如果
结束
结束Su b

但是在这种情况下(不是IsPostPack条件)的问题是下拉列表选择不起作用,例如,如果选择值为"Regular Customer",则无法获得此值.


I have following dropdownlist and search button

<asp:DropDownList ID="CustomerTypeDropList" runat="server"; 
                    

 <asp:Button ID="SearchBtn" runat="server" Text="  Find  " CssClass="myButton" />


This is my code behind in VB.NET

Dim container As New myContainer
 Sub GridViewDisplay(ByVal strSql As String)
  Try
   dropDownStr = "select distinct Cust_Type from [Customers] order by Cust_Type asc"
   textfield = "Cust_Type"
   valuefield = "Cust_Type"
   container.dropDownListBind(CustDropList, dropDownStr, textfield, valuefield)
   Catch ex As Exception
      resultLbl.Text = ex.Message
  End Try
 End Sub


my data-bind sub is in separate following class

public class Mycontainer

Sub dropDownListBind(ByVal myDropDown As DropDownList, ByVal SQLstring As String, ByVal textfield As String, ByVal valuefield As String)
        myConnectionString = WebConfigurationManager.ConnectionStrings("CustConnectStr").ConnectionString
        myCommand.Connection = myConnect
        If myConnect.State = ConnectionState.Closed Then
            myConnect.ConnectionString = myConnectionString
            myConnect.Open()
        End If

        myCommand.CommandText = SQLstring
        'myConnect = ConnectToDatabase(SQLstring)
        myDropDown.DataTextField = textfield
        myDropDown.DataValueField = valuefield
               
        myreader = myCommand.ExecuteReader()
        myDropDown.DataSource = myreader
        myDropDown.DataBind()
         myreader.Close()
        myConnect.Close()
    End Sub
End Class



The event click must be in postback because the user have to choose the type of the customer after first loading of the page.
My error is when the user chooses the customer type and click the search button the selected value in the dropdownlist disappears (dropdownlist viewstate doesn''t work)
how I can solve this error?

解决方案

When you bind to a data source, the selection is lost. You SHOULD do this in a data layer, but at a minimum use if (!IsPostback) ( I don''t know how to do ''not'' in VB, is it just the word ? ) to check if you are in a postback, and if you are, then don''t reload the data needlessly.


Have you put (IsPostBack) in your Page_load if not then put it and then fill dropdownlist


Yes I put this condition in following search Button Sub, but the error is not page_load event.

Protected Sub SearchBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SearchBtn.Click

If Not IsPsotBack then
GridViewDisplay (strSql )

End if

End Su b

but the problem in this case (Not IsPostPack condition) is the dropdownlist selection doesn''t working for example if the selection value is "Regular Customer" I cann''t get this value.


这篇关于单击事件按钮后,下拉列表选择的值消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆