【问题标题】:Getting value of dropdownlist that's added onrowdatabound asp.net c#获取在rowdatabound asp.net c#中添加的下拉列表的值
【发布时间】:2018-04-16 10:04:37
【问题描述】:

我正在查询一个用值填充 gridview 的数据库,还将下拉框添加到 dataview 'onrowdatabound' 内的每个单元格中,以便在填充 gridview 时填充这些 DDL。

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

我认为它们消失了,因为它们没有在页面加载时被调用(我似乎无法这样做,因为它们被称为 onrowdatabound)

<asp:GridView id="View" name="Spview" onrowdatabound="populateCellswithDDls" runat="server"></asp:GridView>

使用循环每个单元格的“populateCellswithDDls”函数添加 ddl:

 e.Row.Cells[i].Controls.Add(DDL1);

接下来我要玩的是 ViewState 和 Sessions 以在回发时保存下拉列表(尝试在 '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);

我已经尝试了各种方法来处理视图状态和会话,但不确定在哪里使用它们来保存“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?

谢谢

【问题讨论】:

  • 你在page_load中绑定你的gridview吗?

标签: c# asp.net session gridview dynamic


【解决方案1】:

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

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

现在在 RowDataBound 事件中确保 DropDownList 有一个ID

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

【讨论】:

  • 感谢您的回答,这看起来不错,但我的数据源是 onrowdatabound 函数中的数据表 - 我如何将其附加到网格视图中的控件.. 我似乎无法要调用函数中的值,我可能会尝试使用静态变量..
  • DDL 的数据源与此 sn-p 的工作无关。但是如果你使用DataTable在方法外声明它,检查它是否为null或为空,如果为空填充一次,然后你可以在每一行中重复使用它。
  • 编辑:静态工作,但是第一次点击 DDL 什么都不做,第二次点击有,这很奇怪'
  • 在数据绑定方法中添加DDL1.AutoPostBack = true
  • 已经是真的了,认为这与查询sql之前页面加载中的数据绑定有关吗?
【解决方案2】:

可能对您有用而不是尝试会话的方法是使用隐藏字段。 为每个下拉位置创建一个隐藏字段,然后使用 javascript 使用下拉值填充隐藏字段。 (可能希望在提交时进行一些服务器端验证)

类似这样的 jQuery:

$(".dropdowns").on("change", function () {
    $(this).closest('input:hidden').val($(this).val());
});

在您的确认中:

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

【讨论】:

  • 我实际上并没有考虑使用 jQuery,我会考虑使用它并让你知道谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-12
相关资源
最近更新 更多