我有一个GridView,它当前显示所有记录(Event表中的所有事件)。现在我想添加一个显示每个事件的下拉框,然后添加一个按钮。单击按钮后,GridView将需要更新该事件id的数据。
我曾经尝试过,但遇到了问题。下面是我的代码,在问题出现之前。我将如何实现下拉菜单?
当前ASP.NET代码
<%@ Page Title="" Language="C#" MasterPageFile="~/admin/admin.master" AutoEventWireup="true" CodeFile="viewregistrant.aspx.cs" Inherits="admin_Default5" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="RegistrantId"
OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit"
OnRowUpdating="OnRowUpdating" OnRowDeleting="OnRowDeleting"
EmptyDataText="No records has been added." CellPadding="4" ForeColor="#333333"
GridLines="None" AllowPaging="True" OnPageIndexChanging="PagingRegistrant_PageIndexChanging" AllowSorting="True">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="First Name" ItemStyle-Width="80">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%# Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Eval("FirstName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle Width="150px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" ItemStyle-Width="80">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLastName" runat="server" Text='<%# Eval("LastName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle Width="150px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address Line 1" ItemStyle-Width="80">
<ItemTemplate>
<asp:Label ID="lblAddressLine1" runat="server" Text='<%# Eval("AddressLine1") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtAddressLine1" runat="server" Text='<%# Eval("AddressLine1") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle Width="150px"></ItemStyle>
</asp:TemplateField>
<asp:CommandField ButtonType="Link" ShowEditButton="true"
ShowDeleteButton="true" ItemStyle-Width="150">
<ItemStyle Width="150px"></ItemStyle>
</asp:CommandField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</asp:Content>
当前C#代码
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.Configuration;
using System.Data.SqlClient;
public partial class admin_Default5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["Events2"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("spRegistrantsGridViewOld"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "SELECT");
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
//Edit Button
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.BindGrid();
}
//Update Button
protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
int registrantId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
string firstName = (row.FindControl("txtFirstName") as TextBox).Text;
string lastName = (row.FindControl("txtLastName") as TextBox).Text;
string constr = ConfigurationManager.ConnectionStrings["Events2"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("spRegistrantsGridViewOld"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "UPDATE");
cmd.Parameters.AddWithValue("@RegistrantId", registrantId);
cmd.Parameters.AddWithValue("@FirstName", firstName);
cmd.Parameters.AddWithValue("@LastName", lastName);
//cmd.Parameters.AddWithValue("@AddressLine1", addressLine1);
//cmd.Parameters.AddWithValue("@AddressLine2", addressLine2);
//cmd.Parameters.AddWithValue("@City", city);
//cmd.Parameters.AddWithValue("@State", state);
//cmd.Parameters.AddWithValue("@Zip", zip);
//cmd.Parameters.AddWithValue("@Country", country);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
GridView1.EditIndex = -1;
this.BindGrid();
}
//Cancel Edit Button
protected void OnRowCancelingEdit(object sender, EventArgs e)
{
GridView1.EditIndex = -1;
this.BindGrid();
}
//Delete Button
protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
int registrantId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
string constr = ConfigurationManager.ConnectionStrings["Events2"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("spRegistrantsGridViewOld"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "DELETE");
cmd.Parameters.AddWithValue("@RegistrantId", registrantId);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
this.BindGrid();
}
//JavaScript confirm delete
//protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
//{
// if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != GridView1.EditIndex)
// {
// (e.Row.Cells[2].Controls[2] as LinkButton).Attributes["onclick"] = "return confirm('Do you want to delete this row?');";
// }
//}
protected void PagingRegistrant_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
DataBind();
}
protected void DropDownListEvent_SelectedIndexChanged(object sender, EventArgs e)
{
GridView1.DataBind();
}
}
我尝试的代码在下面不起作用。。。此处需要一些指针
我知道我需要以某种方式添加这个来填充下拉列表。。。
if (!this.IsPostBack)
{
string events = DropDownListEvent.SelectedValue;
using (SqlConnection sqlConn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["Events2"].ConnectionString))
{
sqlConn2.Open();
using (SqlCommand sqlCmd2 = new SqlCommand())
{
sqlCmd2.Connection = sqlConn2;
sqlCmd2.CommandType = System.Data.CommandType.Text;
sqlCmd2.CommandType = System.Data.CommandType.StoredProcedure;
sqlCmd2.CommandText = "spGetAllEvents";
sqlCmd2.ExecuteNonQuery();
SqlDataReader sqlReader = sqlCmd2.ExecuteReader();
if (sqlReader.HasRows)
{
DropDownListEvent.DataSource = sqlReader;
DropDownListEvent.DataTextField = "EventName";
DropDownListEvent.DataValueField = "EventId";
DropDownListEvent.DataBind();
}
sqlConn2.Close();
}
}
这是为了让它在点击按钮后工作。。。
protected void ButtonChangeEvent_Click(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["Events2"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("spRegistrantsGridView"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "SELECT");
cmd.Parameters.Add("@EventId", SqlDbType.Int).Value = DropDownListEvent.SelectedValue;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
用ASP的下拉列表和按钮。。。
Choose event:
<asp:DropDownList ID="DropDownListEvent" runat="server">
</asp:DropDownList>
<asp:Button ID="ButtonChangeEvent" runat="server"
onclick="ButtonChangeEvent_Click" Text="Button" />
这次尝试没有成功(和其他尝试一样)。。。我真的很想得到一些指导来让这件事发挥作用。非常感谢。
如注释中所述,在EditItemTemplate
标记中添加一个下拉列表控件,如下所示:-
<asp:TemplateField HeaderText="Events" ItemStyle-Width="80">
<ItemTemplate>
<asp:Label ID="lblEvent" runat="server" Text='<%# Eval("EventName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlEvents" runat="server"
SelectedValue='<%# Eval("EventName") %>'></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
然后,在网格视图中添加RowDataBoundEvent
:-
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
OnRowDataBound="GridView1_RowDataBound"
在代码隐藏文件中,添加rowDataBound事件处理程序方法,找到我们添加并绑定的下拉列表:-
protected void grdEmployees_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState && DataControlRowState.Edit > 0)
{
DropDownList ddList= (DropDownList)e.Row.FindControl("ddlEvents");
//Now call your ADO.NET code and bind the dropdownlist.
}
}