使用CommandArgument访问GridView的内部DropDownList.SelectedValue

时间:2012-07-02 00:01:26

标签: c# asp.net .net gridview

当DropDownList“eventnoteEditDrpDwnLst”的值设置为“Custom ...”时,我想在GridView中将Textible“eventnoteAddTextBox”设置为“Visible = true”

<asp:GridView ID="grid" runat="server"
...
    OnRowCommand="grid_RowCommand"
    ShowFooter="True">
<columns>
...
<asp:TemplateField HeaderText="Event">
<ItemTemplate>
    <asp:Label ID="eventnoteLbl" runat="server" Text='<%# Bind("eventnote") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
    <asp:DropDownList ID="eventnoteEditDrpDwnLst" runat="server" DataTextField="eventnote" DataValueField="eventnote"></asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
    <asp:DropDownList ID="eventnoteAddDrpDwnLst" runat="server" DataTextField="eventnote" DataValueField="eventnote" CommandName="AddCustomEventNote" CommandArgument="<%# ??? eventnoteAddDrpDwnLst.SelectedText ??? %>"></asp:DropDownList>
    <asp:UpdatePanel ID="eventnoteAddUpdatePanel" runat="server" RenderMode="Block">
    <ContentTemplate>
        <asp:TextBox ID="eventnoteAddTextBox" runat="server" DataTextField="eventnote" DataValueField="eventnote" Visible="False"></asp:TextBox>
    </ContentTemplate>
    </asp:UpdatePanel>
</FooterTemplate>
</asp:TemplateField>
...
</columns>
</asp:GridView>

代码隐藏

protected void grid_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("AddCustomEventNote"))
    {
        DropDownList eventnoteAddDrpDwnLst = (DropDownList)e.CommandSource;
        if (eventnoteAddDrpDwnLst.SelectedValue == "Custom...")
            Response.Write("<script>alert('It works!');</script>");
            //grid.FooterRow.FindControl("eventnoteAddTextBox").Visible = true;
    }
}

这不起作用。

2 个答案:

答案 0 :(得分:1)

这应该这样做

protected void grid_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("AddCustomEventNote"))
    {
        DropDownList eventnoteAddDrpDwnLst = (DropDownList)grid.FooterRow.FindControl("eventnoteAddDrpDwnLst");

        string value = eventnoteAddDrpDwnLst.SelectedValue;
    }
}

答案 1 :(得分:0)

实际上应该有<asp:DropDownList>的AutoPostBack和单独的调用函数。它也可以用<asp:UpdatePanel RenderMode="Block"><asp:ScriptManager EnablePartialRendering="true">覆盖,以便顺利进行。

<asp:DropDownList ID="eventnoteAddDrpDwnLst" runat="server" DataTextField="eventnote" DataValueField="eventnote" AutoPostBack="true" OnSelectedIndexChanged="eventnoteAddDrpDwnLst_SelectedIndexChanged" />

代码隐藏非常简单:

protected void eventnoteAddDrpDwnLst_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList eventnoteAddDrpDwnLst = (DropDownList)grid.FooterRow.FindControl("eventnoteAddDrpDwnLst");
        if (eventnoteAddDrpDwnLst.SelectedValue == "Custom...")
            grid.FooterRow.FindControl("eventnoteAddTextBox").Visible = true;
        else
            grid.FooterRow.FindControl("eventnoteAddTextBox").Visible = false;
    }

真的有效! DropDownList没有CommandArgument!