DROPDOWNLIST在GridView的价值 [英] Dropdownlist in Gridview value

查看:210
本文介绍了DROPDOWNLIST在GridView的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态地添加值,这是在GridView中列之一DROPDOWNLIST。

问题是,在下拉列表中选择的值时,增加了一个新的行不保存或维护?任何想法,我可能做错了。这里是code,我有..

下面是我的GridView

 < ASP:GridView控件ID =GridView1=服务器的cellpadding =4前景色=#333333网格=无OnDataBound =GridView1_DataBound>
                    < AlternatingRowStyle背景色=白/>
                    <柱体和GT;
                        < ASP:BoundField的数据字段=IDHEADERTEXT =类别ID/>
< ASP:BoundField的数据字段=univirsityHEADERTEXT =类别名称/>                        < ASP:的TemplateField HEADERTEXT =产品>
<&ItemTemplate中GT;
< ASP:DropDownList的ID =dropdownlist1=服务器>
< / ASP:DropDownList的>
< / ItemTemplate中>
< / ASP:的TemplateField>
                    < /专栏>                < / ASP:GridView的>

下面是code结合的数据的GridView单击一个按钮时,动态

 如果(!Page.IsPostBack)
            {
                DataTable的DT =新的DataTable();
                dt.Columns.Add(ID,System.Type.GetType(System.Int32));
                dt.Columns.Add(univirsity,System.Type.GetType(System.Int32));
                dt.Columns.Add(重大,System.Type.GetType(System.Int32));
                会话[MyDataTable] = DT;
            }        }        保护无效的button1_Click(对象发件人,EventArgs的发送)
        {
            数据表T =(数据表)会议[MyDataTable];
            DataRow的ROW1 = t.NewRow();            ROW1 [ID] = t.Rows.Count + 1;
            ROW1 [univirsity] = 3;
           // ROW1 [主要] = 31;
            t.Rows.Add(ROW1);            会话[MyDataTable] = T;
            GridView1.DataSource = T;
            GridView1.DataBind();
        } 保护无效GridView1_DataBound(对象发件人,EventArgs的发送)
        {
            //的AddControl();            ArrayList的列表=新的ArrayList();
            list.Add(DMG);
            list.Add(SVC);            的foreach(在GridView1.Rows GridViewRow行)
            {
                // INT键=(int)的GridView1.DataKeys [row.RowIndex] .value的;
                如果(row.RowType == DataControlRowType.DataRow)
                {
                    DropDownList的DL =(DropDownList的)row.FindControl(dropdownlist1);                    字符串SEL = dl.SelectedValue;
                    如果(!String.IsNullOrEmpty(SEL))
                    {
                        dl.DataSource =清单;
                        dl.DataBind();
                        dl.SelectedValue = SEL;
                    }
                    其他
                    {
                        dl.DataSource =清单;
                        dl.DataBind();                    }                }
            }
        }


解决方案

您不是的的任何错误正因为如此,你只是希望错误的事情。如Gri​​dView的模板控件也能这样;如果你希望你的模板内的控制的设定值持续下去,你必须自己做。

我劝不存储大量的会话数据,但我会假设你知道,你的情况是不打算扩展到数百万有几行用户的的DataTable。每。

在你的情况,我会另列添加到的DataTable存储产品价值;与每次回发时同步它,并重新绑定确保您设置表中的值DropDownList的值: -

 保护覆盖无效的OnLoad(EventArgs的发送)
{
    如果(!Page.IsPostBack)
    {
        DataTable的DT =新的DataTable();
        dt.Columns.Add(ID的typeof(INT));
        dt.Columns.Add(univirsity的typeof(INT));
        dt.Columns.Add(重大的typeof(INT));
        //我们的新列
        dt.Columns.Add(selectedProduct,typeof运算(字符串));
        会话[MyDataTable] = DT;
    }
    其他
    {
        //在每次回发,循环通过网格和更新
        //数据表        //获取我们的餐桌AsEnumerable,所以我们可以在其上使用LINQ前pressions
        VAR表=((数据表)会议[MyDataTable])AsEnumerable()。        的foreach(GridViewRow gridRow在GridView1.Rows)
        {
            //从电网获得此行的ID
            INT ID = Convert.ToInt32(gridRow.Cells [0]。文本);            //并从表中的数据行匹配
            VAR的TableRow = table.Where(X =>(INT)×[ID] == ID)
                                .SingleOrDefault();            如果(tablerow的!= NULL)
            {
                VAR DDL =(DropDownList的)gridRow.FindControl(dropdownlist1);
                tablerow的[selectedProduct] = ddl.SelectedValue;
            }
        }
    }    base.OnLoad(E);
}无效的button1_Click(对象发件人,EventArgs的发送)
{
    数据表T =(数据表)会议[MyDataTable];
    DataRow的ROW1 = t.NewRow();    ROW1 [ID] = t.Rows.Count + 1;
    ROW1 [univirsity] = 3;
    新列//设置默认值
    ROW1 [selectedProduct] =DMG;    t.Rows.Add(ROW1);    会话[MyDataTable] = T;
    GridView1.DataSource = T;
    GridView1.DataBind();
}保护无效GridView1_DataBound(对象发件人,EventArgs的发送)
{
    ArrayList的列表=新的ArrayList();
    list.Add(DMG);
    list.Add(SVC);    //再次得到我们的表
    VAR表=((数据表)会议[MyDataTable])AsEnumerable()。    的foreach(GridViewRow gridRow在GridView1.Rows)
    {
        如果(gridRow.RowType == DataControlRowType.DataRow)
        {
            //让我们的ID和再次的TableRow
            INT ID = Convert.ToInt32(gridRow.Cells [0]。文本);
            VAR的TableRow = table.Where(X =>(INT)×[ID] == ID)
                                .SingleOrDefault();            //绑定列表
            DropDownList的DL =(DropDownList的)gridRow.FindControl(dropdownlist1);
            dl.DataSource =清单;
            dl.DataBind();            从tablerow.selectedProduct //设定值
            dl.SelectedValue =(字符串)的TableRow [selectedProduct];
        }
    }
}

I am dynamically adding values to the dropDownlist which is one of the column in Gridview.

The problem is the value selected in dropdown list is not saved or maintained when a new row is added ?. Any idea what I could be doing wrong. Here is the code which I have ..

Here is my Gridview

   <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" OnDataBound="GridView1_DataBound">
                    <AlternatingRowStyle BackColor="White" />
                    <Columns>
                        <asp:boundfield datafield="ID" headertext="categoryid" />
<asp:boundfield datafield="univirsity" headertext="category name" />

                        <asp:templatefield headertext="products">
<itemtemplate>
<asp:dropdownlist id="dropdownlist1" runat="server">
</asp:dropdownlist>
</itemtemplate>
</asp:templatefield>
                    </Columns>

                </asp:GridView>

Here is the code which binds the data to Gridview dynamically when a button is clicked.

if (!Page.IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("ID", System.Type.GetType("System.Int32"));
                dt.Columns.Add("univirsity", System.Type.GetType("System.Int32"));
                dt.Columns.Add("major", System.Type.GetType("System.Int32"));
                Session["MyDataTable"] = dt;
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DataTable t = (DataTable)Session["MyDataTable"];
            DataRow row1 = t.NewRow();

            row1["ID"] = t.Rows.Count + 1;
            row1["univirsity"] = 3;
           // row1["major"] = 31;
            t.Rows.Add(row1);

            Session["MyDataTable"] = t;
            GridView1.DataSource = t;
            GridView1.DataBind(); 
        }

 protected void GridView1_DataBound(object sender, EventArgs e)
        {
            //addControl();

            ArrayList list = new ArrayList();
            list.Add("DMG");
            list.Add("SVC");

            foreach (GridViewRow row in GridView1.Rows)
            {
                // int key = (int)GridView1.DataKeys[row.RowIndex].Value;
                if (row.RowType == DataControlRowType.DataRow)
                {
                    DropDownList dl = (DropDownList)row.FindControl("dropdownlist1");

                    string sel = dl.SelectedValue;
                    if (!String.IsNullOrEmpty(sel))
                    {
                        dl.DataSource = list;
                        dl.DataBind();
                        dl.SelectedValue = sel;
                    }
                    else
                    {
                        dl.DataSource = list;
                        dl.DataBind();

                    }

                }
            }
        }

解决方案

You're not doing anything 'wrong' as such, you're just expecting the wrong thing. Templated controls such as gridviews function this way; if you want the selected value of a control within your template to persist, you'll have to do it yourself.

I would advise not storing large amounts of data in Session but I'm going to assume you know that, and your scenario is not intended to scale to millions of users with several rows in that datatable.. each.

In your case, I would add another column to that datatable to store the 'product' value; sync it with each postback, and on re-binding make sure you set the dropdownlist value from the table value: -

protected override void OnLoad(EventArgs e)
{
    if (!Page.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ID", typeof(int));
        dt.Columns.Add("univirsity", typeof(int));
        dt.Columns.Add("major", typeof(int));
        // our new column
        dt.Columns.Add("selectedProduct", typeof(string));
        Session["MyDataTable"] = dt;
    }
    else
    {
        // on each postback, loop through the grid and update
        // the datatable

        // get our table AsEnumerable, so we can use LINQ expressions on it
        var table = ((DataTable)Session["MyDataTable"]).AsEnumerable();

        foreach (GridViewRow gridRow in GridView1.Rows)
        {
            // get this row's ID from the grid
            int id = Convert.ToInt32(gridRow.Cells[0].Text);

            // and get the matching datarow from the table
            var tableRow = table.Where(x => (int)x["ID"] == id)
                                .SingleOrDefault();

            if (tableRow != null)
            {
                var ddl = (DropDownList)gridRow.FindControl("dropdownlist1");
                tableRow["selectedProduct"] = ddl.SelectedValue;
            }
        }
    }

    base.OnLoad(e);
}

void button1_Click(object sender, EventArgs e)
{
    DataTable t = (DataTable)Session["MyDataTable"];
    DataRow   row1 = t.NewRow();

    row1["ID"] = t.Rows.Count + 1;
    row1["univirsity"] = 3;
    // set default value for new column
    row1["selectedProduct"] = "DMG";

    t.Rows.Add(row1);

    Session["MyDataTable"] = t;
    GridView1.DataSource = t;
    GridView1.DataBind();
}

protected void GridView1_DataBound(object sender, EventArgs e)
{
    ArrayList list = new ArrayList();
    list.Add("DMG");
    list.Add("SVC");

    // get our table again
    var table = ((DataTable)Session["MyDataTable"]).AsEnumerable();

    foreach (GridViewRow gridRow in GridView1.Rows)
    {
        if (gridRow.RowType == DataControlRowType.DataRow)
        {
            // get our id and tablerow again
            int id = Convert.ToInt32(gridRow.Cells[0].Text);
            var tableRow = table.Where(x => (int)x["ID"] == id)
                                .SingleOrDefault();

            // bind the list
            DropDownList dl = (DropDownList)gridRow.FindControl("dropdownlist1");
            dl.DataSource = list;
            dl.DataBind();

            // set value from tablerow.selectedProduct
            dl.SelectedValue = (string)tableRow["selectedProduct"];
        }
    }
}

这篇关于DROPDOWNLIST在GridView的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆