GridView 每行邦定 Dropdownlist 控件,然后点页面的“确定”按钮,如何把每行的dropdownlist中的值保存下来?

时间:2022-05-10 14:44:08
页面:
<asp:GridView ID="DtgView" runat="server" AutoGenerateColumns="False" Width="98%"  Height=98%
                 BorderStyle="None" GridLines="Horizontal" frame="void" AllowPaging="True"  EnableViewState="false"
                 OnRowDataBound="DtgView_RowDataBound">
                <Columns>
                            
                    <asp:BoundField DataField="EquiId" HeaderText="EquiId" Visible=false />         
                    
                     
                    <asp:TemplateField HeaderText="设备名称">
                        <ItemTemplate>
                            <%#GetEquipName(DataBinder.Eval(Container.DataItem, "EquiId").ToString())%>                            
                        </ItemTemplate>
                        <ItemStyle Width="30%" />
                    </asp:TemplateField > 
                    
                    
                    
                    <asp:TemplateField HeaderText="确认状态">
                        <ItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
                        </ItemTemplate>
                        <ItemStyle Width="10%" />
                    </asp:TemplateField >  

<tr>
                <td align=center>
                    <asp:Button runat="server" ID="btn_save" Text="保 存" CssClass="NewButton" CausesValidation="false" OnClick="btn_save_Click" />
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <asp:Button runat="server" ID="btn_submit" Text="确 定" CssClass="NewButton" CausesValidation="false"  OnClick="btn_submit_Click"  />
                </td>                
            </tr>

服务器:
protected void DtgView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");                        
            ddl.DataSource = task.GetConfirmStatus();
            ddl.DataTextField = "StatusName";
            ddl.DataValueField = "id";
            ddl.DataBind();
            ListItem li = new ListItem("请选择", "0");
            ddl.Items.Insert(0, li);
        }
    }

    protected void btn_save_Click(object sender, EventArgs e)
    {        
        //这块怎么写????急!
    }

我需要点击“确定键”,将所有的dropdownlist里的值和对应的行的equiid保存到数据库中。不知道该用gridview中的哪个事件能做到,谁能教教我?多谢!

6 个解决方案

#1


   <asp:DropDownList ID="ddlProductType" runat="server">
   </asp:DropDownList>
   <asp:HiddenField ID="HidTypeID" Value='<%# Eval("主键Id") %>' runat="server" />
   后台循环遍历每一行 得到数据 

#2


i=0;i<Gridview1.Rows.count;i++
GridView1.rows[i].cells[2]

#3


我先试一试。多谢ls两位。关键是后台遍历是用gridview的事件还是自己写循环?

#4


不行啊,得到的是这些:
System.Web.UI.WebControls.DataControlFieldCell; System.Web.UI.WebControls.DataControlFieldCell; System.Web.UI.WebControls.DataControlFieldCell; System.Web.UI.WebControls.DataControlFieldCell; System.Web.UI.WebControls.DataControlFieldCell; System.Web.UI.WebControls.DataControlFieldCell; System.Web.UI.WebControls.DataControlFieldCell; System.Web.UI.WebControls.DataControlFieldCell; System.Web.UI.WebControls.DataControlFieldCell; System.Web.UI.WebControls.DataControlFieldCell 

#5


该回复于2009-04-23 17:27:32被版主删除

#6


可以了!
这么写就可以了:
protected void btn_save_Click(object sender, EventArgs e)
    {
        string var = "";
        for (int i = 0; i < DtgView.Rows.Count; i++)
        {
            var = var + ";  " + ((DropDownList)DtgView.Rows[i].Cells[2].FindControl("DropDownList1")).SelectedValue;
        }
        Response.Write(var);
    }

结果为:; 1; 2; 0; 0; 0; 0; 0; 0; 0; 0 

多谢!

#1


   <asp:DropDownList ID="ddlProductType" runat="server">
   </asp:DropDownList>
   <asp:HiddenField ID="HidTypeID" Value='<%# Eval("主键Id") %>' runat="server" />
   后台循环遍历每一行 得到数据 

#2


i=0;i<Gridview1.Rows.count;i++
GridView1.rows[i].cells[2]

#3


我先试一试。多谢ls两位。关键是后台遍历是用gridview的事件还是自己写循环?

#4


不行啊,得到的是这些:
System.Web.UI.WebControls.DataControlFieldCell; System.Web.UI.WebControls.DataControlFieldCell; System.Web.UI.WebControls.DataControlFieldCell; System.Web.UI.WebControls.DataControlFieldCell; System.Web.UI.WebControls.DataControlFieldCell; System.Web.UI.WebControls.DataControlFieldCell; System.Web.UI.WebControls.DataControlFieldCell; System.Web.UI.WebControls.DataControlFieldCell; System.Web.UI.WebControls.DataControlFieldCell; System.Web.UI.WebControls.DataControlFieldCell 

#5


该回复于2009-04-23 17:27:32被版主删除

#6


可以了!
这么写就可以了:
protected void btn_save_Click(object sender, EventArgs e)
    {
        string var = "";
        for (int i = 0; i < DtgView.Rows.Count; i++)
        {
            var = var + ";  " + ((DropDownList)DtgView.Rows[i].Cells[2].FindControl("DropDownList1")).SelectedValue;
        }
        Response.Write(var);
    }

结果为:; 1; 2; 0; 0; 0; 0; 0; 0; 0; 0 

多谢!