2011-05-06 147 views
5

我一直在尋找一個解決方案,但一段時間,看到很多帖子,告訴我如何做到這一點,但我不能讓我的SelectedIndexChanged事件觸發DropDownList時更改。GridView中的DropDownList SelectedIndexChanged不能觸發!

DropDownList控件的AutoPostBack被設置爲True,我一直緊跟在後下也代碼: Link to post

這裏是我的代碼:

.ASPX

<asp:GridView ID="gvCases" DataKeyNames="UserId" runat="server" AutoGenerateColumns="False" 
    BorderWidth="0px" CssClass="gridList" GridLines="None"> 
    <AlternatingRowStyle BackColor="#F7F7F7" /> 
    <Columns> 

     <asp:BoundField DataField="id" HeaderText="Case Ref" /> 

     <asp:TemplateField HeaderText="Name"> 
      <ItemTemplate> 
       <asp:Label ID="clientName" runat="server" Text="Label"></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 

     <asp:BoundField DataField="company" HeaderText="Company" /> 

     <asp:TemplateField HeaderText="Order Date"> 
      <ItemTemplate> 
       <asp:Label ID="dateTime" runat="server" Text="Label"></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 

     <asp:TemplateField HeaderText="Case Owner"> 
      <ItemTemplate> 
       <asp:DropDownList ID="iconUsers" runat="server" OnSelectedIndexChanged="iconUsers_SelectedIndexChanged"> 
       </asp:DropDownList> 
      </ItemTemplate> 
     </asp:TemplateField> 

     <asp:TemplateField ShowHeader="False"> 
      <ItemTemplate> 
       <asp:Button ID="btnDetails" runat="server" CausesValidation="False" Text="Details" /> 
      </ItemTemplate> 
     </asp:TemplateField> 

     <asp:TemplateField ShowHeader="False"> 
      <ItemTemplate> 
       <asp:Button ID="btnSchedule" runat="server" CausesValidation="False" Text="Schedule" /> 
      </ItemTemplate> 
     </asp:TemplateField> 



    </Columns> 
</asp:GridView> 

.VB

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


    If (Request.IsAuthenticated = False) Then 
     Response.Redirect("~/admin/default.aspx") 
    End If 


    Dim keypadSQL As SqlConnection = New SqlConnection() 
    keypadSQL.ConnectionString = ConfigurationManager.ConnectionStrings("connKeypad").ConnectionString() 


    Dim cmdActive As SqlCommand = New SqlCommand() 
    cmdActive.Connection = keypadSQL 
    cmdActive.CommandText = "spCasesActive" 
    cmdActive.CommandType = CommandType.StoredProcedure 


    Dim daCases As SqlDataAdapter = New SqlDataAdapter 
    daCases.SelectCommand = cmdActive 

    Dim dsCases As DataSet = New DataSet() 
    daCases.Fill(dsCases, "CaseList") 

    Dim CaseTotal As Integer 
    CaseTotal = dsCases.Tables(0).Rows.Count 

    If CaseTotal = 1 Then 
     iCaseTotal.InnerHtml = CaseTotal & " Case" 
    Else 
     iCaseTotal.InnerHtml = CaseTotal & " Cases" 
    End If 

    gvCases.DataSource = dsCases 
    gvCases.DataBind() 
    cmdActive.Dispose() 


    If Page.IsPostBack Then 

    End If 

End Sub 

Protected Sub gvCases_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvCases.RowDataBound 

    If e.Row.RowType = DataControlRowType.Header Then 

     gvCases.Columns(5).ItemStyle.Width() = "60" 
     gvCases.Columns(6).ItemStyle.Width() = "70" 

    End If 

    If e.Row.RowType = DataControlRowType.DataRow Then 

     Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView) 

     Dim strClientName As String 
     Dim clientName As Label 
     strClientName = rowView("firstname") & " " & rowView("lastname") 
     clientName = CType(e.Row.FindControl("clientName"), Label) 
     clientName.Text = strClientName 

     Dim strDateTime As String 
     Dim dateTime As Label 
     strDateTime = rowView("CaseSent") 
     dateTime = CType(e.Row.FindControl("dateTime"), Label) 
     dateTime.Text = FormatDateTime(strDateTime, DateFormat.ShortDate) & "<br />" & FormatDateTime(strDateTime, DateFormat.ShortTime) 

     gvCases.Columns(3).ItemStyle.Font.Size = 8 
     gvCases.Columns(5).ControlStyle.CssClass = "btnEdit" 
     gvCases.Columns(6).ControlStyle.CssClass = "btnSchedule" 

     Dim intUserId As String 
     intUserId = Convert.ToString(gvCases.DataKeys(e.Row.RowIndex).Value) 



     Dim cmd As New SqlCommand("SELECT id, Firstname, Lastname, Firstname + ' ' + Lastname As FullName FROM [users_icon] ORDER BY Firstname, Lastname", New SqlConnection(ConfigurationManager.ConnectionStrings("connKeypad").ConnectionString())) 
     cmd.Connection.Open() 

     Dim ddlValues As SqlDataReader 
     ddlValues = cmd.ExecuteReader() 

     Dim iconUsers As DropDownList 
     iconUsers = CType(e.Row.FindControl("iconUsers"), DropDownList) 
     iconUsers.Style.Add("font-size", "11px") 
     iconUsers.DataSource = ddlValues 
     iconUsers.DataValueField = "id" 
     iconUsers.DataTextField = "FullName" 
     iconUsers.DataBind() 

     Dim ListItem1 = New ListItem("Select Case Owner", "0") 
     iconUsers.Items.Insert("0", ListItem1) 
     iconUsers.AutoPostBack = True 
     If IsDBNull(rowView("CaseOwner")) Then 
      iconUsers.SelectedValue = 0 
     Else 
      iconUsers.SelectedValue = rowView("CaseOwner") 
     End If 

     AddHandler iconUsers.SelectedIndexChanged, AddressOf iconUsers_SelectedIndexChanged 

     cmd.Connection.Close() 
     cmd.Connection.Dispose() 


     Dim btnDetails As Button = CType(e.Row.FindControl("btnDetails"), Button) 
     btnDetails.PostBackUrl = "~/admin/detail.aspx?uid=" & intUserId 

     Dim LabelAddress As Button = CType(e.Row.FindControl("btnSchedule"), Button) 
     LabelAddress.PostBackUrl = "~/admin/schedule.aspx?uid=" & intUserId 

    End If 

End Sub 

Protected Sub iconUsers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) 

    Response.Write("Function Called") 

End Sub 

感謝您的任何幫助。 J.

+0

是的,在gvCases_RowDataBound - iconUsers.AutoPostBack = True – JBoom 2011-05-06 13:14:57

+0

是啓用還是禁用ViewState? – Jeff 2011-05-06 13:37:12

+0

設置爲在gridview和dropdownmenu上繼承,應該只設置爲頁面的默認值,所以我猜這是啓用!? – JBoom 2011-05-06 13:45:20

回答

6

有一些類似的問題(見Event handler not firing using AddHandlerAssign an event to a custom control inside a Repeater control),但你的具體情況看起來就像是增加了處理器的兩倍;一次在標記中,一次在數據綁定中。

我想刪除RowDataBound事件中的一個(因爲它沒有做任何事情,因爲處理程序將在您回發後丟失,並且事件實際上會觸發後添加處理程序)。另外,請確保您的AutoPostBack爲@Bala提及。

+1

我已經稍微改變了我的代碼,我已經把事件處理程序和autopostback放在代碼後面,並放在我的.ASPX中 - ,但它仍然沒有發射:(,我很好,真的卡住了。 – JBoom 2011-05-06 13:36:12

+0

因此,該頁面發回,你正在調試,並且它從來沒有達到你在iconUsers_SelectedIndexChanged設置的斷點? – 2011-05-06 14:03:33

+0

是啊 – JBoom 2011-05-06 14:08:51

4

你說AutoPostBack設置爲true但我沒有在標記中看到它,並且默認情況下它被設置爲false。因此,嘗試

<asp:DropDownList ID="iconUsers" runat="server" OnSelectedIndexChanged="iconUsers_SelectedIndexChanged" AutoPostBack="true"> 
      </asp:DropDownList> 
+0

嗨巴拉,它在代碼後面在gvCases_RowDataBound這一行:iconUsers.AutoPostBack = True。當我更改下拉列表時,我可以看到該頁面正在回傳,但事件不會觸發。 – JBoom 2011-05-06 13:34:50

相關問題