ASP.NET从代码隐藏中的EditItemTemplate中的DropDownList获取值

时间:2022-11-15 04:02:02

I have a GridView that I have placed in DropDownList's in 2 columns.

我有一个GridView,我放在DropDownList的2列中。

 <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());
    }

2 个解决方案

#1


3  

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);

#2


2  

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.

我没有测试代码。你可能需要调整。

#1


3  

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);

#2


2  

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.

我没有测试代码。你可能需要调整。