简体   繁体   中英

ASP.NET Gridview Dropdown Template Field Triggering SelectedIndexChanged on Gridview

I have a basic ASP.NET Gridview w/ a ddlAction dropdownlist

    <asp:GridView ID="GridView1" CssClass="mgrid2"
    runat="server" AutoGenerateColumns="False" ForeColor="White" DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:BoundField DataField="ClientName" HeaderText="ClientName" SortExpression="ClientName" />
        <asp:BoundField DataField="ProjectID" HeaderText="ProjectID" SortExpression="ProjectID" />
        <asp:BoundField DataField="ProjectName" HeaderText="ProjectName" SortExpression="ProjectName" />
        <asp:BoundField DataField="Budget" HeaderText="Budget" ReadOnly="True" SortExpression="Budget" />
        <asp:BoundField DataField="PercentComplete" HeaderText="PercentComplete" ReadOnly="True" SortExpression="PercentComplete" />
        <asp:BoundField DataField="EarnedMH" HeaderText="EarnedMH" ReadOnly="True" SortExpression="EarnedMH" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:DropDownList ID="ddlAction" CssClass="dropdownlist" runat="server" AutoPostBack="true" OnSelectedIndexChanged=""GridView1_SelectedIndexChanged"></asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

It is populated on rowdatabound

   If e.Row.RowType = DataControlRowType.DataRow Then
        Dim ddl = TryCast(e.Row.FindControl("ddlAction"), DropDownList)
        If ddl IsNot Nothing Then
            ddl.DataSource = New List(Of String)() From {
                "Action",
                "View",
                "Edit",
                "Archive"
            }
            ddl.DataBind()
   End IF

I would like for the dropdownlist to trigger the GridView1 selectedindexchanged event so I can get the selected row's ProjectID, but I would also like to get the selected value of the dropdownlist ddlAction

Similar to this example Thank you

UPDATED w/ Answer

Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
    Dim ddl = DirectCast(sender, DropDownList)
    Dim row As GridViewRow = CType(ddl.NamingContainer, GridViewRow)

From these two items you can access what you need

我有一段时间没有使用vb了,但是您没有在事件方法中访问发件人的权限吗?

var value = (sender as DropDownList).SelectedValue;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM