2011-09-12 31 views
1

我有一個gridview內的下拉列表。現在我想要點擊一個按鈕時,我可以檢查dropdownlist的值。我已經解僱的GridView的rowcommand事件,但調試器是不能去達到there..Please幫助me..My代碼是如何通過點擊一個按鈕來獲取GridView中的dropdownlist的值?

Protected Sub grd_Test_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grd_Test.RowCommand 
    If e.CommandName = "Select" Then 


    End If 
End Sub 

我的源代碼是

<asp:GridView ID="grd_UnAssignProperties" runat="server" AutoGenerateColumns="False"><Columns> 
        <asp:TemplateField HeaderText="Assign To"> 
         <ItemTemplate> 
          <asp:DropDownList ID="drp_UnAssignProp" runat="server"> 

          <asp:ListItem Value="" >Default</asp:ListItem> 
          </asp:DropDownList> 
         </ItemTemplate> 
        </asp:TemplateField> 
       </Columns></asp:GridView><tr><td><asp:Button ID="btn_Save" runat="server" CommandName="Select" Text="Submit" /> 

回答

1

試試這個

DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[0].FindControl("drp_UnAssignProp"); 
string val = ddl.SelectedValue; 
+0

先生,這將是下一步但是我的rowcommand也沒有firing..can妳告訴我ÿ?如果你發現錯誤,請告訴我,我已經寫完了所有的代碼。 –

+1

先生,我認爲e.RowIndex不是rowcommand的成員.... –

0

首先,因爲按鈕btn_Save不是GridView內,點擊它會不會提高grd_Test_RowCommand,無論是移動內部GridView按鈕或你必須手動提高這樣的:

從asp.net論壇複製:

Protected Sub Button1_Click(sender As Object, e As EventArgs) 
    Dim commandArgs As New CommandEventArgs("Command Name Here", "Your Command Argument Here") 
    'You can pass any row 
    'You can also skip the row parameter and get the row from Command Argument 
    Dim eventArgs As New GridViewCommandEventArgs(GridView1.Rows(0), GridView1, commandArgs) 
    GridView1_RowCommand(GridView1, eventArgs) 
End Sub 

現在關於你原來的問題,這是你可以進去RowCommand事件DropDownList選擇的值:

編輯:固定的代碼來獲取當前行,爲其RowCommand引發事件

Protected Sub grd_Test_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grd_Test.RowCommand 
    If e.CommandName = "Select" Then 
     Dim index As Integer = Convert.ToInt32(e.CommandArgument) 
     Dim row As GridViewRow = DirectCast(DirectCast(e.CommandSource, LinkButton).NamingContainer, GridViewRow) 
     Dim ddl as DropDownList = CType(row.FindControl("drp_UnAssignProp", DropDownList) 
     Dim selectedValue as String = ddl.Text 
    End If 
End Sub 
+0

它更接近我想要的。但是有一個錯誤是「無法將類型爲'System.EventArgs'的對象轉換爲鍵入System.IConvertible」。在'If e.CommandName =「Select」Then Dim index As Integer = Convert.ToInt32(e.CommandArgument)' –

+0

你可以刪除這行:Dim index As Integer = Convert.ToInt32(e.CommandArgument)它的沒用我忘了刪除。檢查並讓我知道 – Waqas

+0

先生刪除此行後有一個錯誤,即「無法將類型'System.Web.UI.WebControls.GridView'類型的對象鍵入'System.Web.UI.WebControls.LinkBut​​ton'」。 –

1

嘗試

string val = (DropDownList)GridView1.Rows[e.RowIndex].Cells[0] 
       .FindControl("drp_UnAssignProp").SelectedValue; 
相關問題