ASP.NET 2.0的 GridView中模板列绑定DropDownList的数据处理

发布时间:2016-12-9 6:38:21 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"ASP.NET 2.0的 GridView中模板列绑定DropDownList的数据处理",主要涉及到ASP.NET 2.0的 GridView中模板列绑定DropDownList的数据处理方面的内容,对于ASP.NET 2.0的 GridView中模板列绑定DropDownList的数据处理感兴趣的同学可以参考一下。

现在正在做的一个模块中, 需要对GridView中的记录进行下拉框赋值, 常用的方法就是用模板列绑一个DropDownList, <asp:templatefield headertext="VALUE">                                <itemtemplate><asp:dropdownlist id="DropDownList1" runat="server" /></itemtemplate></asp:templatefield>   然后在onrowdatabound方法中初始化ddl里的Items, 并根据记录的Value来设定ddl的SelectValue.       获取值时, 通过遍历GridView的所有Row,Find到ddl来得到用户更新了的Value,并将它Update到DB中.    又或者是给一个UpdateCommond到GridView中,通过onrowupdating方法来更新  (((dropdownlist))gridview1.rows[e.rowindex].findcontrol("DropDownList1")).selectevalue;  第一种获取值的方法需要提供一个更新的途径,并且无论ddl中的值是否改变,都要整个的Update一次, 第二种途径不用逐个更新,但每修改一条记录中的ddl就要去Click它的Update Button.  我希望达到的目的是,用户修改了哪条记录里的ddl,我就Update那条, 当然最好是不知不觉中, 当然ddl不能是选一次 就Update一下数据库,默认在autopostback为false时,也不会这样,但是在GridView中好像很难直接达到这种效果, 经过尝试发现GridView+WebUserControl能够达到比较理想的效果. WebUserControl.ascx: <%...@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="Demo_GV_WUC_WebUserControl" %><div style="white-space:nowrap">    <asp:dropdownlist id="ddlValue" runat="server"         onselectedindexchanged="ddlValue_SelectedIndexChanged" />    </div>后台: public partial class Demo_GV_WUC_WebUserControl : System.Web.UI.UserControl...{    /**//// <summary>    /// 主键ID    /// </summary>    public int PID    ...{        set ...{ ViewState["Id"] = value; }        get ...{ return ViewState["Id"] == null ? 0 : Int32.Parse(ViewState["Id"].ToString()); }    }     /**//// <summary>    /// 下拉框赋值    /// </summary>    public string Value    ...{        set ...{ ViewState["Value"] = value; }        get ...{ return ViewState["Value"] == null ? "0" : ViewState["Value"].ToString(); }    }     protected void Page_Load(object sender, EventArgs e)    ...{        if (!IsPostBack)        ...{            ddlValue.Items.Add(new ListItem("中国", "0"));            ddlValue.Items.Add(new ListItem("美国", "1"));            ddlValue.Items.Add(new ListItem("法国", "2"));             ddlValue.SelectedValue = Value;        }    }     protected void ddlValue_SelectedIndexChanged(object sender, EventArgs e)    ...{        string sql = @"UPDATE [MySpace].[dbo].[T4] SET [Value] = '{0}' WHERE [ID] = {1}";         using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MySpaceDB"].ConnectionString))        ...{            conn.Open();            SqlCommand comm = new SqlCommand(string.Format(sql, ddlValue.SelectedValue, PID.ToString()), conn);            if (comm.ExecuteNonQuery() > 0)            ...{                   Thread.Sleep(500);            }        }            }}GridView.aspx: <%...@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Demo_GV_WUC_Default" %> <%...@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title>GridView with WebUserControl</title>    <style type="text/css" media="screen">...        *        {...}{            magin: 0;            padding: 0;        }        .block        {...}{            margin: 10px;            padding: 10px;            border: 1px solid #d1d1d1;        }    </style></head><body>    <form id="form1" runat="server">    <asp:scriptmanager id="ScriptManager1" runat="server">    </asp:scriptmanager>    <asp:updatepanel id="UpdatePanel1" runat="server">        <contenttemplate>            <div style="width: 80%; margin: 20px auto auto auto;">                <div class="block">                    <asp:label id="Label1" runat="server" font-bold="True" font-size="Large" forecolor="#6699FF"                        text="GridView + WebUserControl"></asp:label>                </div>                                <div class="block">                    <asp:gridview id="GridView1" autogeneratecolumns="false"  runat="server">                        <columns>                            <asp:boundfield datafield="id" headertext="ID" />                                                        <asp:templatefield headertext="VALUE">                                                                <itemtemplate>                                    <uc1:webusercontrol id="myWebUserControl" PID='<%# Eval("ID")%>' Value='<%...# Eval("Value")%>' runat="server" />                                </itemtemplate>                             </asp:templatefield>                        </columns>                    </asp:gridview>                    <asp:button id="Button1" runat="server" text="PostBack" />                </div>            </div>                </contenttemplate>    </asp:updatepanel>    </form></body></html> 后台: public partial class Demo_GV_WUC_Default : System.Web.UI.Page...{        protected void Page_Load(object sender, EventArgs e)    ...{        if (!IsPostBack)        ...{            string sql = @"SELECT [ID],[Value] FROM [MySpace].[dbo].[T4]";             using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MySpaceDB"].ConnectionString))            ...{                SqlDataAdapter adp = new SqlDataAdapter(sql, conn);                DataSet ds = new DataSet();                adp.Fill(ds);                if (ds.Tables.Count > 0)                ...{                    GridView1.DataSource = ds.Tables[0];                    GridView1.DataBind();                    Thread.Sleep(1000);                }                          }             }    }}    

上一篇:GridView的RowCommand里取列的值
下一篇:Visual Studio 2010以及TeamFoundationServer 2010 MSDN免Key版地址分享

相关文章

相关评论