简体   繁体   English

ASP.NET从背后的EditItemTemplate中的DropDownList获取值

[英]ASP.NET Get value from DropDownList in EditItemTemplate in codebehind

I have a GridView that I have placed in DropDownList's in 2 columns. 我有一个放置在DropDownList的2列中的GridView。

 <asp:TemplateField HeaderText="Upgrade" SortExpression="Upgrade">
                <ItemTemplate>
                    <asp:Label ID="LabelUpgrade" runat="server" Text='<%# Eval("Upgrade") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlUpgrade" runat="server" Width="100px">
                        <asp:ListItem Value="1">--Select--</asp:ListItem>
                        <asp:ListItem Value="2">1</asp:ListItem>
                        <asp:ListItem Value="3">2</asp:ListItem>
                        <asp:ListItem Value="4">3</asp:ListItem>
                        <asp:ListItem Value="5">4</asp:ListItem>
                        <asp:ListItem Value="6">5</asp:ListItem>
                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>

how do I grab the item from ddlUpgrade in the codebehind? 我该如何从ddlUpgrade的后台代码中获取项目?

OnUpdating Event - I don't have a way to pull the row to get the value from the drop down but I add my sql parameters here. OnUpdating事件-我没有办法拉行以从下拉列表中获取值,但是我在这里添加了sql参数。

 protected void IAP_Updating(object sender, SqlDataSourceCommandEventArgs e){}

RowUpdating Event - I can get the row here but I can't add the value to the sql parameters because e.command isn't valid here RowUpdating事件-我可以在这里获取行,但不能将值添加到sql参数中,因为e.command在这里无效

protected void gvClients_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow _row = gvClients.Rows[e.RowIndex];

        DropDownList _ddl = (DropDownList)_row.FindControl("ddlUpgrade");
        SqlParameter _parm = new SqlParameter("@Upgrade", _ddl.SelectedItem.ToString());
    }

On the RowUpdating event you can capture the control inside the edit template based on its ID. 在RowUpdating事件上,您可以根据其ID在编辑模板内捕获控件。

GridViewRow row = GridView1.Rows[e.RowIndex];

DropDownList ddl = (DropDownList)row.FindControl("ddlUpgrade");

SqlParameter _parm = new SqlParameter("@Upgrade", ddl.SelectedItem.ToString());
    e.Command.Parameters.Add(_parm);

I would add a hidden field outside the GridView: 我将在GridView之外添加一个隐藏字段:

<asp:HiddenField ID="hdnSelection" value="" runat="server" />

And change the gvClients_RowUpdating method: 并更改gvClients_RowUpdating方法:

protected void gvClients_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow _row = gvClients.Rows[e.RowIndex];        
    DropDownList _ddl = _row.FindControl("ddlUpgrade") as DropDownList;

    if(_ddl != null)
    {
        hdnSelection.Value = _ddl.SelectedItem.Text;
        IAP.Update();//Assuming IAP is the ID of the SqlDataSource
    }

}

And my IAP_Updating method should look like this: 我的IAP_Updating方法应如下所示:

protected void IAP_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
    SqlParameter _parm = new SqlParameter("@Upgrade", hdnSelection.Value);
    e.Command.Parameters.Add(_parm);
}

I did not test the code. 我没有测试代码。 You may need to tweak. 您可能需要调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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