在Gridview中加入Dropdownlist模板列,加入DropDownlist 是一种常用的操作,其中涉及到如何获取选择项和Gridview重新绑定两个要点。
如图
前台代码如下
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %> <!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 id="Head1" runat="server">
<title></title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script language="javascript" type="text/javascript"> function vary() { var table = document.getElementById("<%=grv1.ClientID %>");
var finished = 1;
for (var i = 1; i < table.rows.length; i++) {
var filled = table.rows[i].cells[1].getElementsByTagName("select")[0].value;
if (filled == 0) { finished = 0; break; }
}
if (finished == 0) { alert("Self Not finished"); return false; }
else return true;
} function vary2() {
var finished = 1;
var selects = document.getElementsByTagName("select");
if (selects.length > 0)
for (var i = 0; i < selects.length; i++) {
if (selects[i].id.indexOf("ddlself", 0) > 0)
{
if (selects[i].value == 0)
{ finished = 0; break; }
} }
if (finished == 0) { alert("Not finished!"); return false; }
else return true; }
function vary3() { var finish = 1;
$('table[id$=grv1]').find('tr').each(function (row) {
$ddl = $(this).find('select');
if ($ddl != "undefined") {
var d = $ddl.val();
if (d == 0) { finish = 0; }
} });
if (finish == 0) { alert("Not finished!"); return false; }
else return true;
} function test() {
var select = document.getElementById("ddl");
if (select.value == 0) { alert("not finished"); return false; }
else return true;
} // $(document).ready(function(){$("input[id$=btnSave]").click(function(){vary3();});})
</script> </head> <body> <form id="form1" runat="server">
<div>
<table width="100%"><tr >
<td width="50%" align="center"><asp:gridview ID="grv1" runat="server"
GridColor="Black" AutoGenerateColumns="False"
onprerender="grv1_PreRender" >
<Columns>
<asp:BoundField HeaderText="Comments" DataField="Name" />
<asp:TemplateField HeaderText="Self">
<ItemTemplate>
<asp:DropDownList ID="ddlself" runat="server">
<asp:ListItem Value="0" Text="N/A"></asp:ListItem>
<asp:ListItem Value="1" Text="1"></asp:ListItem>
<asp:ListItem Value="2" Text="2"></asp:ListItem>
<asp:ListItem Value="3" Text="3"></asp:ListItem>
<asp:ListItem Value="4" Text="4"></asp:ListItem>
<asp:ListItem Value="5" Text="5"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Supervisor">
<ItemTemplate>
<asp:DropDownList ID="ddlsupervisor" runat="server">
<asp:ListItem Value="0" Text="N/A"></asp:ListItem>
<asp:ListItem Value="1" Text="1"></asp:ListItem>
<asp:ListItem Value="2" Text="2"></asp:ListItem>
<asp:ListItem Value="3" Text="3"></asp:ListItem>
<asp:ListItem Value="4" Text="4"></asp:ListItem>
<asp:ListItem Value="5" Text="5"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview></td><td width="50%"></td></tr>
</table>
<table width="100%">
<tr><td width ="30%"></td><td width ="30%"><asp:Button ID="btnSave" Text="Save"
runat="server" onclick="btnSave_Click"/></td><td></td></tr>
</table>
</div>
</form>
</body>
</html>
其中三个Js代码段均能实现功能,不过作为三个Jq,尚有不足,虽然undefined的引入使其功能可以实现,不过总归不是正统的解决办法,希望可以改进。
后台代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack)
{
Info a = new Info();
a.Name = "a";
Info b = new Info();
b.Name = "b";
Info c = new Info();
c.Name = "c";
Info d = new Info();
d.Name = "d";
List<Info> list = new List<Info>(); list.Add(a);
list.Add(b);
list.Add(c);
list.Add(d);
grv1.DataSource = list;
grv1.DataBind(); btnSave.Attributes.Add("onclick", "javascript:vary();");
}
} protected void grv1_PreRender(object sender, EventArgs e)
{
if (ViewState["drp1"] != null) Set(grv1);
}
protected void Set(GridView grvItem)
{
List<string> list = new List<string>();
list = (List<string>)ViewState["drp1"];
for (int row = ; row < grvItem.Rows.Count; row++)
{
DropDownList ddlSelf = (DropDownList)grvItem.Rows[row].FindControl("ddlself");
ddlSelf.SelectedValue = list[row].ToString(); }
}
protected void btnSave_Click(object sender, EventArgs e)
{ List<string> list = new List<string>();
for (int row = ; row < grv1.Rows.Count; row++)
{
DropDownList ddlSelf = (DropDownList)grv1.Rows[row].Cells[].FindControl("ddlself");
string selectindex = ddlSelf.SelectedValue;
list.Add(selectindex);
}
ViewState["drp1"] = new List<string>();
ViewState["drp1"] = list; } }
}