C# 获取添加到RowDataBound asp.net c上的dropdownlist的值

C# 获取添加到RowDataBound asp.net c上的dropdownlist的值,c#,asp.net,session,gridview,dynamic,C#,Asp.net,Session,Gridview,Dynamic,我正在查询一个数据库,在gridview中填充值,并在dataview“onrowdatabound”中的每个单元格中添加下拉框,以便在填充gridview时填充这些DDL 我希望能够单击一个按钮从这些DDL中获取值,但是当单击该按钮时,会发生回发,并且所有DDL都会消失,它会为我提供DDL的默认值 我假设它们消失了,因为它们没有在pageload上调用,而我似乎不能像它们在RowDataBond上调用一样 <asp:GridView id="View" name="Spview" onr

我正在查询一个数据库,在gridview中填充值,并在dataview“onrowdatabound”中的每个单元格中添加下拉框,以便在填充gridview时填充这些DDL

我希望能够单击一个按钮从这些DDL中获取值,但是当单击该按钮时,会发生回发,并且所有DDL都会消失,它会为我提供DDL的默认值

我假设它们消失了,因为它们没有在pageload上调用,而我似乎不能像它们在RowDataBond上调用一样

<asp:GridView id="View" name="Spview" onrowdatabound="populateCellswithDDls" runat="server"></asp:GridView>
接下来我要玩的是ViewState和Sessions,它在postback上保存dropdownlists,并在“populateCellswithDDls”函数中创建会话,如下所示:

DropDownList DDL1 = new DropDownList();

//I've tried newSkillsMon.AutoPostBack = true; but this just removes them all too

Session.Add("ViewState", View);
Session.Add("DropdownState", DDL1);
我已经尝试了viewstate和session的各种方法,但不确定在保存“onrowdatabound”填充的状态时在哪里使用它们

我的按钮当前看起来像这样:

 protected void confirm_Click(object sender, EventArgs e){

                {foreach (GridViewRow row in View.Rows)

                        // if (DDL1.SelectedItem.Value != "Select Item"){

                        if (IsPostBack)
                        {
                            Debug.WriteLine(DDL1.SelectedValue);
                        }
这只给了我X数量的选择项,而不是我在DDL中选择的

我缺少什么,我应该在哪里添加这些会话来保持onrowdatabound创建的ddl


谢谢

使用隐藏字段可能比尝试会话更适合您。 为每个下拉位置创建一个隐藏字段,然后使用javascript用下拉值填充隐藏字段。可能需要在提交时进行一些服务器端验证

jQuery中的类似内容:

$(".dropdowns").on("change", function () {
    $(this).closest('input:hidden').val($(this).val());
});
在您的确认中:

if (HF_HiddenField1.Value != "Select Item")

创建动态控件时,您需要在每次页面加载时重新创建它们,这包括回发。因此,首先在IsPostBack检查之外绑定GridView数据

现在在RowDataBound事件中,确保DropDownList具有ID

现在,您可以通过单击按钮从正确的行中获取值

protected void Button1_Click(object sender, EventArgs e)
{
    //find the dropdownlist in the correct row and cast it back to one
    DropDownList DDL1 = GridView1.Rows[i].FindControl("DDL1") as DropDownList;

    //display the result
    Label1.Text = DDL1.SelectedValue;
}

您是否在page_load中绑定了gridview?我实际上没有考虑使用jQuery,我将研究如何使用它,并让您知道谢谢您的回答,这看起来很棒,但我的数据源是onrowdatabound函数中的一个datatable-我如何将其附加到grid view中的控件中。。我似乎无法调用函数中的值,我可能尝试使用静态变量..DDL的数据源与此代码段的工作无关。但是,如果您使用DataTable在方法外部声明它,请检查它是否为null或空,如果为空,请填充一次,然后您可以在每一行中重用它。编辑:静态工作,但是第一次单击DDL不起任何作用,第二次单击则起作用,这很奇怪'Add DDL1.AutoPostBack=true到数据绑定方法。已经是true,是否认为这与查询sql之前页面加载中的数据绑定有关?
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //normally you would bind here
    }

    //but now bind grid every page load
    GridView1.DataSource = Common.LoadFromDB();
    GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //create a dropdownlist and assign an ID
        DropDownList DDL1 = new DropDownList();
        DDL1.ID = "DDL1";

        //add some dummy listitems
        DDL1.Items.Insert(0, new ListItem() { Text = "A", Value = "A" });
        DDL1.Items.Insert(1, new ListItem() { Text = "B", Value = "B" });
        DDL1.Items.Insert(2, new ListItem() { Text = "C", Value = "C" });

        //add the control to the row
        e.Row.Cells[0].Controls.Add(DDL1);
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    //find the dropdownlist in the correct row and cast it back to one
    DropDownList DDL1 = GridView1.Rows[i].FindControl("DDL1") as DropDownList;

    //display the result
    Label1.Text = DDL1.SelectedValue;
}