2012-11-26 76 views
0

請參閱下面的代碼:ASP.NET - 模板字段下拉列表

Protected Sub GVHistoricNames_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GVHistoricNames.RowUpdating 
      Try 
       'Dim strUsercode As String = GVHistoricNames.Rows(e.RowIndex).Cells(2).Text 
       Dim ddl As DropDownList = CType(GVHistoricNames.Rows(e.RowIndex).FindControl("ddlUsercode"), DropDownList) 
       MsgBox(ddl.SelectedIndex) 


      Catch ex As Exception 
       'I don't swallow exceptions 
      Finally 

      End Try 
     End Sub 

我想獲得一個下拉列表中選擇的值。模板字段的定義如下:

<asp:TemplateField HeaderText="Usercode"> 
       <ItemTemplate> 
       <asp:DropDownList ID="ddlUsercode" runat="server" SelectedValue ='<%# Bind("Usercode") %>' DataTextField="Usercode" DataValueField="Usercode"> 
       </asp:DropDownList> 
       </ItemTemplate> 
      </asp:TemplateField> 

MSGBOX(ddl.SelectedIndex)始終打印在一個空字符串:GVHistoricNames_RowUpdatinGVHistoricNames_RowUpdating。我在做什麼?

回答

0

檢查您是否未綁定或將下拉列表填充到rowUpdating事件之前頁生命週期中的某個位置。確保您使用not isPostback。檢查那裏的語法,因爲我是一個C#人!