gridview如何返回droplistdown数据给数据库
后台代码
public partial class Default : System.Web.UI.Page { public string j; SqlConnection sqlcon; SqlCommand sqlcom; string strCon = "Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\业务\\App_Data\\yw.mdf;Integrated Security=True;User Instance=True"; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); } }
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; bind(); }
//删除数据 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string sqlstr = "delete from yw where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; sqlcon = new SqlConnection(strCon); sqlcom = new SqlCommand(sqlstr, sqlcon); sqlcon.Open(); sqlcom.ExecuteNonQuery(); sqlcon.Close(); bind(); }
//更新数据 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { sqlcon = new SqlConnection(strCon); string sqlstr = "update yw set yw='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; sqlcom = new SqlCommand(sqlstr, sqlcon); sqlcon.Open(); sqlcom.ExecuteNonQuery(); sqlcon.Close(); GridView1.EditIndex = -1; bind(); }
//取消数据 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; bind(); }
//绑定数据 public void bind() { //修改下面两行代码,注意where后面要有空格 string i; i = DropDownList1.SelectedItem.Text; if (int.Parse(DropDownList1.SelectedValue) == 0) j = "yw= 'aa' or yw= 'bb'"; if (int.Parse(DropDownList1.SelectedValue) == 1) j = "yw='" + i + "'"; if (int.Parse(DropDownList1.SelectedValue) == 2) j = "yw='" + i + "'"; string sqlstr = "select * from yw where " + j; sqlcon = new SqlConnection(strCon); SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon); DataSet myds = new DataSet(); sqlcon.Open(); myda.Fill(myds, "yw"); GridView1.DataSource = myds; GridView1.DataKeyNames = new string[] { "id" };//主键 GridView1.DataBind(); sqlcon.Close(); } protected void Button1_Click(object sender, EventArgs e) { bind(); } } 前台代码 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" > <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <Columns > <asp:BoundField DataField="id" HeaderText="用户ID" ReadOnly="True" /> <asp:BoundField DataField="name" HeaderText="用户姓名" ReadOnly="true" /> <asp:BoundField DataField="tel" HeaderText="联系方式" ReadOnly="true" /> <asp:BoundField DataField="yw" HeaderText="业务情况" /> <asp:BoundField DataField="qb" HeaderText="业务情况" Visible="False" /> <asp:CommandField HeaderText="编辑" ShowEditButton="True" /> <asp:CommandField HeaderText="删除" ShowDeleteButton="True" /> </Columns > <RowStyle ForeColor="#000066" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> </asp:GridView > <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"> <asp:ListItem Value="0">y</asp:ListItem> <asp:ListItem Value="1">aa</asp:ListItem> <asp:ListItem Value="2">bb</asp:ListItem> </asp:DropDownList> <asp:Button ID="Button1" runat="server" Text="查询" onclick="Button1_Click" /> 我该如何更改,使下面的输入框更改为下拉列表,并能将其数据返回到数据库
<asp:TemplateField HeaderText="业务情况"> <ItemTemplate> <%# Eval("业务情况")%> </ItemTemplate> <EditItemTemplate> <asp:HiddenField ID="HDFSex" runat="server" Value='<%# Eval("业务情况") %>' /> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"> <asp:ListItem Value="0">y</asp:ListItem> <asp:ListItem Value="1">aa</asp:ListItem> <asp:ListItem Value="2">bb</asp:ListItem> </asp:DropDownList>
</EditItemTemplate> <ItemStyle Width="100px" /> </asp:TemplateField>
更新你就在这里面写语句啊: protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 获取选择的值 string value = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1")).SelectedValue;
|