【求助】:在GRIDVIEW的模板列中得下拉菜单绑定SQL表中得某个字段,该怎么做?
在用户管理界面中 GRIDVIEW内的用户组列为模板列,该如何绑定,编辑后,用户组列以下拉菜单形式显示,如果在下拉菜单内绑定数据表groupinfo中得字段groupname的内容,默认还必须显示当前用户所在用户组名,我看了坛子里的很多相关帖子,大部分还没看明白,因为我是新人,请能详细按照我的情况说明,最好语句上能有注释,以便我今后学习,谢谢各位~!前台代码:
<%@ Page Title="" Language="C#" MasterPageFile="~/admin/admin.master" AutoEventWireup="true" CodeFile="admin_usermng.aspx.cs" Inherits="admin_admin_usermng" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table cellpadding="0" cellspacing="0" class="style2">
<tr>
<td style="text-align: center">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None"
onrowcommand="GridView1_RowCommand" onrowdatabound="GridView1_RowDataBound"
Width="996px" onrowediting="GridView1_RowEditing"
onrowcancelingedit="GridView1_RowCancelingEdit">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="username" HeaderText="用户名" ReadOnly="True" />
<asp:TemplateField HeaderText="真实姓名">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("realname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbrealname" runat="server" Height="20px"
Text='<%# Bind("realname") %>' Width="88px"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="性别">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("sex") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlsex" runat="server" SelectedValue='<%# Bind("sex") %>'>
<asp:ListItem Value="女"></asp:ListItem>
<asp:ListItem Value="男"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="年龄">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("age") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbage" runat="server" Text='<%# Bind("age") %>' Width="40px"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="联系电话">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("tel") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbtel" runat="server" Text='<%# Bind("tel") %>' Width="100px"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="电子信箱">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("email") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("email") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="用户组">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("groupname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlusergroup" runat="server"
SelectedValue='<%# Eval("groupname") %>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="regdate" HeaderText="注册时间" ReadOnly="True" />
<asp:BoundField DataField="logintimes" HeaderText="登录次数" ReadOnly="True" />
<asp:TemplateField HeaderText="审核">
<ItemTemplate>
<asp:ImageButton ID="cmdcheck" runat="server"
CommandArgument='<%# Eval("id") %>' CommandName="cmdcheck"
ImageUrl='<%# (bool)Eval("ispass")?"~/images/pass.gif":"~/images/nopass.gif" %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Image" CancelImageUrl="~/images/cancle.gif"
EditImageUrl="~/images/edit.gif" HeaderText="修改" ShowEditButton="True"
UpdateImageUrl="~/images/update.gif" />
<asp:TemplateField HeaderText="密码重置">
<ItemTemplate>
<asp:ImageButton ID="cmdpwd0" runat="server"
CommandArgument='<%# Eval("id") %>' CommandName="cmdpwd0"
ImageUrl="~/images/reset.gif" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="删除">
<ItemTemplate>
<asp:ImageButton ID="cmddel" runat="server" CommandArgument='<%# Eval("id") %>'
CommandName="cmddel" ImageUrl="~/images/del.gif" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</td>
</tr>
<tr>
<td class="tip" style="text-align: center">
<strong>【说明】:密码不允许修改,只能通过“密码重置”功能将用户密码初始化为12345。</strong></td>
</tr>
</table>
</asp:Content>
--------------------编程问答--------------------
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class admin_admin_usermng : System.Web.UI.Page
{
SQLHelper _sqluser = new SQLHelper();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!Convert.ToBoolean(_sqluser.ReturnValue("select useradmin from groupinfo where id=(select groupid from userinfo where id=" + Session["UID"].ToString() + ")")))
{
WebCommond.WriteMsg("您没有此项管理权限", "default.aspx");
Response.End();
}
bindGridView1();
}
}
void bindGridView1()
{
_sqluser.BindGridViewReturnCount("select * from View_UserGroup", GridView1, new string[] { "id" });
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName )
{
case "cmdcheck":
_sqluser.RunProc("update userinfo set ispass=~ispass where id="+e.CommandArgument.ToString());
bindGridView1();
break;
case "cmdpwd0":
_sqluser.RunProc("update userinfo set userpwd='12345' where id="+e.CommandArgument.ToString());
bindGridView1();
break;
case "cmddel":
if (Convert.ToBoolean(_sqluser.ReturnValue("select isdel from userinfo where id=" + e.CommandArgument.ToString())))
{
if (_sqluser.ExistDate("select top 1 id from articles where userid=" + e.CommandArgument.ToString()) || _sqluser.ExistDate("select top 1 id from images where userid=" + e.CommandArgument.ToString()))
{
WebCommond.WriteMsg("此用户有文章或图片发布,不能删除!");
}
else
{
_sqluser.RunProc("delete from userinfo where id=" + e.CommandArgument.ToString());
bindGridView1();
}
}
else
{
WebCommond.WriteMsg("此用户不能删除");
}
break;
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)//必须是数据行
{
//为删除按钮加确认对话框
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)//判断当前编辑状态
{
((ImageButton)e.Row.Cells[12].FindControl("cmddel")).Attributes.Add("onclick", "javascript:return confirm('你确认要删除“" + e.Row.Cells[0].Text + "”吗?')");
((ImageButton)e.Row.Cells[11].FindControl("cmdpwd0")).Attributes.Add("onclick", "javascript:return confirm('你确认要重置“" + e.Row.Cells[0].Text + "”的初始密码吗?')");
}
//鼠标移入变背景色
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "oc=this.style.backgroundColor;this.style.backgroundColor='#f8f4d1'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=oc");
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bindGridView1();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bindGridView1();
}
} --------------------编程问答-------------------- 补充说明:用户组的编辑绑定我都没做,不知道该从何入手,如果有朋友愿意教我,请说的详细点~~谢谢!
--------------------编程问答-------------------- 你的结贴率 很刺眼 --------------------编程问答-------------------- 我也是刚知道这个论坛可以学到知识,来了时间很短,见笑了~! --------------------编程问答--------------------
//数据绑定后触发--------------------编程问答-------------------- 果断帮顶 --------------------编程问答--------------------
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//如果行的类型是数据绑定行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//行的状态是: 编辑状态 或者 (交替行且是编辑状态)
if(e.Row.RowState == DataControlRowState.Edit ||
e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit)
)
{
DropDownList ddlSex = e.Row.FindControl("ddlSex") as DropDownList;
if (ddlSex != null)
{
ListItem item1 = new ListItem();
item1.Value = "0";
item1.Text = "女";
ddlSex.Items.Add(item1);
ListItem item2 = new ListItem();
item2.Value = "1";
item2.Text = "男";
ddlSex.Items.Add(item2);
}
我是想知道怎么绑定数据库的字段,不是添加固定的项目~!不过还是很感谢参与讨论。 --------------------编程问答-------------------- 有朋友愿意帮忙一下嘛?
补充:.NET技术 , ASP.NET