GridView的EditItemTemplate中的DropDownList获取的SelectedValue [英] Gridview EditItemTemplate DropDownList Get SelectedValue

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

问题描述

在我的GridView我有以下模板字段:

 < ASP:的TemplateField的HeaderText =系codeSORTEX pression =系code>
    <&ItemTemplate中GT;
        <%#的eval(系code)%GT;
    < / ItemTemplate中>
    <&EditItemTemplate的GT;
        < ASP:DropDownList的ID =ddlDept code=服务器
            的SelectedValue ='<%#的eval(系code)%GT;'
            数据源='<%#GetAllDepartments()%GT;'
            DataTextField =系code
            DataValueField =系code/>
    < / EditItemTemplate中>
< / ASP:的TemplateField>

当我点击编辑某一行上它填充了所有值将DropDownList并选择该行的正确值这个伟大工程。

然而,当我尝试更新该行: OnRowUpdating =UpdateRow

 保护无效UpdateRow(对象发件人,GridViewUpdateEventArgs E)
{
    GridViewRow行= UserGV.Rows [e.RowIndex]
    DropDownList的DDL = row.FindControl(ddlDept code)作为DropDownList的;
    串部门code = ddl.SelectedValue;
}

它发现DropDownList控件,但SelectedValue的始终是一个空字符串。结果
我需要访问到所选择的值,保存到数据库中。

任何想法,我怎么能得到一个DropDownList的SelectedValue的在GridView在code后面?

编辑:结果
您还可以填充从后面code将DropDownList和SelectedValue的:

 保护无效gv_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
     如果(e.Row.RowType == DataControlRowType.DataRow)
     {
        如果((e.Row.RowState&放大器; DataControlRowState.Edit)大于0)
        {
           VAR deptMgr =新DepartmentMgr();
           清单<&系GT;部门= deptMgr.GetAllDepartments();
           DropDownList的ddList =(DropDownList的)e.Row.FindControl(ddlDept code);
           ddList.DataSource =部门;
           ddList.DataTextField =系code;
           ddList.DataValueField =系code;
           ddList.DataBind();           。字符串userDept code =的DataBinder.Eval(e.Row.DataItem,部code)的ToString();
           ddList.SelectedItem.Text = userDept code;
           ddList.SelectedValue = userDept code;
       }
    }
}


解决方案

我是用一个黑客位,以获得对表标题第二头时绑定GridView的:

  GridViewRow行=新GridViewRow(0,-1,DataControlRowType.Header,DataControlRowState.Normal);
第的TableCell =新TableHeaderCell();
th.Horizo​​ntalAlign = Horizo​​ntalAlign.Center;
th.ColumnSpan = UserGV.Columns.Count;
th.BackColor = Color.SteelBlue;
th.ForeColor = Color.White;
th.Font.Bold = TRUE;
th.Text =管理用户;
row.Cells.Add(次);
InnerTable.Rows.AddAt(0,行);

我不完全了解这是与得到一个DropDownList控件的SelectedValue的,但只要干扰我评论说出来它开始工作。

对于那些有兴趣,我得到了第二头与该工作采用了不同的方法:

在我添加这到GridView .aspx文件:

  OnRowCreated =CreateRow

而在code后面我加了下面的方法:

 保护无效CreateRow(对象发件人,GridViewRowEventArgs E)
{
    如果(e.Row.RowType == DataControlRowType.Header)
    {
        GridView控件的GridView =(GridView的)发件人;
        GridViewRow行=新GridViewRow(1,0,DataControlRowType.Header,DataControlRowState.Normal);
        第的TableCell =新TableHeaderCell();
        th.Horizo​​ntalAlign = Horizo​​ntalAlign.Center;
        th.ColumnSpan = UserGV.Columns.Count;
        th.ForeColor = Color.White;
        th.BackColor = Color.SteelBlue;
        th.Font.Bold = TRUE;
        th.Text =管理用户;
        row.Cells.Add(次);        gridView.Controls [0] .Controls.AddAt(0,行);
    }
}

现在一切正常。

In my Gridview I have the following template field:

<asp:TemplateField HeaderText="Dept Code" SortExpression="DeptCode">
    <ItemTemplate>
        <%# Eval("DeptCode") %>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:DropDownList ID="ddlDeptCode" runat="server" 
            SelectedValue='<%# Eval("DeptCode") %>' 
            DataSource='<%# GetAllDepartments() %>' 
            DataTextField="DeptCode" 
            DataValueField="DeptCode" />
    </EditItemTemplate>
</asp:TemplateField> 

This works great when I click Edit on a row it populates the DropDownList with all values and selects the correct value for that row.

However, when I try to update the row: OnRowUpdating="UpdateRow"

protected void UpdateRow(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = UserGV.Rows[e.RowIndex];
    DropDownList ddl = row.FindControl("ddlDeptCode") as DropDownList;
    string deptCode = ddl.SelectedValue;
}

It finds the DropDownList control but the SelectedValue is always an empty string.
I need access to the selected value to save to the database.

Any ideas as to how I can get the SelectedValue of a DropDownList in a Gridview in code behind?

Edit:
You can also populate the DropDownList and SelectedValue from the code behind:

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
           var deptMgr = new DepartmentMgr();
           List<Department> departments = deptMgr.GetAllDepartments();
           DropDownList ddList = (DropDownList)e.Row.FindControl("ddlDeptCode");
           ddList.DataSource = departments;
           ddList.DataTextField = "DeptCode";
           ddList.DataValueField = "DeptCode";
           ddList.DataBind();

           string userDeptCode = DataBinder.Eval(e.Row.DataItem, "DeptCode").ToString();
           ddList.SelectedItem.Text = userDeptCode;
           ddList.SelectedValue = userDeptCode;
       }
    }
}

解决方案

I was using a bit of a hack to get a second header for the table title when binding the gridview:

GridViewRow row = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal);
TableCell th = new TableHeaderCell();
th.HorizontalAlign = HorizontalAlign.Center;
th.ColumnSpan = UserGV.Columns.Count;
th.BackColor = Color.SteelBlue;
th.ForeColor = Color.White;
th.Font.Bold = true;
th.Text = "Manage Users";
row.Cells.Add(th);
InnerTable.Rows.AddAt(0, row);

I don't completely understand how this was interfering with getting the SelectedValue of a DropDownList control but as soon as I commented that out it started working.

For those interested I got the second header working with this using a different approach:

In the .aspx file I added this to the Gridview:

OnRowCreated="CreateRow"

And in the code behind I added the following method:

protected void CreateRow(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {           
        GridView gridView = (GridView)sender;
        GridViewRow row = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Normal);
        TableCell th = new TableHeaderCell();
        th.HorizontalAlign = HorizontalAlign.Center;
        th.ColumnSpan = UserGV.Columns.Count;
        th.ForeColor = Color.White;
        th.BackColor = Color.SteelBlue;
        th.Font.Bold = true;
        th.Text = "Manage Users";
        row.Cells.Add(th);

        gridView.Controls[0].Controls.AddAt(0, row);
    }
}

Everything is working correctly now.

这篇关于GridView的EditItemTemplate中的DropDownList获取的SelectedValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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