页面加载时,显示选中或未选中的复选框
Posted
技术标签:
【中文标题】页面加载时,显示选中或未选中的复选框【英文标题】:Upon Pageload, Show Checkbox Checked Or Unchecked 【发布时间】:2011-10-02 20:29:54 【问题描述】:我在复选框的网格视图中有一个列。我希望能够根据数据库中的相应值显示这些框以选中或取消选中(目前此列未绑定到任何字段,我遇到了问题)。一直给我带来问题的是我正在使用 Ajax。当我执行 AutoPostBack = true 时,该复选框只显示了一会儿,然后当它刷新所有内容时,该复选框就会消失。任何帮助将不胜感激。
.CS
public partial class vieworders : System.Web.UI.Page
private string orderByString;
private string fieldString;
private string address;
private DataGrid dataGrid = new DataGrid();
protected void Page_Load(object sender, EventArgs e)
orderByString = orderByList.SelectedItem.Value;
fieldString = searchTextBox.Text;
string sqlStatement = "SELECT fName,lName,zip,email,cwaSource,price,length FROM SecureOrders WHERE fName LIKE '%" + fieldString + "%' OR lName LIKE'%" + fieldString + "%' OR zip LIKE'%" + fieldString + "%' OR zip LIKE'%" + fieldString + "%' OR email LIKE'%" + fieldString + "%' OR cwaSource LIKE'%" + fieldString + "%' OR length LIKE'%" + fieldString + "%' OR price LIKE'%" + fieldString + "%' ORDER BY " + orderByString;
////////////////////////////
System.Configuration.Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager.
OpenWebConfiguration("/Cabot3");
System.Configuration.ConnectionStringSettings connectionString;
connectionString = rootWebConfig.ConnectionStrings.ConnectionStrings["secureodb"];
//TEST
for (int i = 0; i < DefaultGrid.Rows.Count; i++)
CheckBox chkUpdate = (CheckBox)DefaultGrid.Rows[i].Cells[1].FindControl("CheckBoxProcess");
if (chkUpdate != null)
OrderBrowser.Text += "Test";
// Create an SqlConnection to the database.
using (SqlConnection connection = new SqlConnection(connectionString.ToString()))
connection.Open();
SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlStatement, connection);
// create an SqlCommandBuilder - this will automatically generate the
// commands, and set the appropriate properties in the dataAdapter
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
// create the DataSet
DataSet dataSet = new DataSet();
// fill the DataSet using our DataAdapter
dataAdapter.Fill(dataSet, "SecureOrders");
SqlCommand cmd = new SqlCommand("SELECT * FROM SecureOrders", connection); // might not need this
SqlCommand bitCmd = new SqlCommand("Select IsNull(processed,0) as processedField From SecureOrders", connection);
DataView source = new DataView(dataSet.Tables[0]);
DefaultGrid.DataSource = source;
DefaultGrid.DataBind();
protected void DefaultGrid_SelectedIndexChanged(Object sender, EventArgs e)
GridViewRow row = DefaultGrid.SelectedRow;
string name = "Name: " + row.Cells[2].Text + " " + row.Cells[3].Text + "\r\n";
// if (row.Cells[4].Text == " ")
//
//address = "Address: " + row.Cells[3].Text + "\r\n " + row.Cells[5].Text + ", " + row.Cells[6].Text + " " + row.Cells[7].Text + " " + row.Cells[8].Text + "\r\n";
//
//else
//
// address = "Address: " + row.Cells[3].Text + "\r\n " + row.Cells[4].Text + "\r\n " + row.Cells[5].Text + ", " + row.Cells[6].Text + " " + row.Cells[7].Text + " " + row.Cells[8].Text + "\r\n";
//
string zip = "Zip: " + row.Cells[4].Text + "\r\n";
string email = "Email: " + row.Cells[5].Text + "\r\n";
//string phone = "Phone: " + row.Cells[10].Text + "\r\n";
//string cctype = "Credit Card Type: " + row.Cells[11].Text + "\r\n";
//string ccnum = "Credit Card Number: " + row.Cells[12].Text + "\r\n";
//string ccexp = "Credit Card Expiration: " + row.Cells[13].Text + "\r\n";
string length = "Length: " + row.Cells[8].Text + "\r\n";
//string delivery = "Delivery: " + row.Cells[15].Text + "\r\n";
string price = "Price: " + row.Cells[7].Text + "\r\n";
string source = "Source: " + row.Cells[6].Text + "\r\n";
//string joined = "Joined: " + row.Cells[18].Text + "\r\n";
//string url = "URL: " + row.Cells[19].Text + "\r\n";
OrderBrowser.Text = name + email + length + price + source;
protected void CheckBoxProcess_CheckedChanged(object sender, EventArgs e)
CheckBox cb = (CheckBox)sender;
GridViewRow gr = (GridViewRow)cb.NamingContainer;
if (cb.Checked)
OrderBrowser.Text = "checked";
else
OrderBrowser.Text = "unchecked";
.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="vieworders.aspx.cs" Inherits="Cabot3.custserv.vieworders" %>
<asp:DropDownList runat="server" ID="orderByList" AutoPostBack="true">
<asp:ListItem Value="fName" Selected="True">First Name</asp:ListItem>
<asp:ListItem Value="lName">Last Name</asp:ListItem>
<asp:ListItem Value="state">State</asp:ListItem>
<asp:ListItem Value="zip">Zip Code</asp:ListItem>
<asp:ListItem Value="cwaSource">Source</asp:ListItem>
<asp:ListItem Value="cwaJoined">Date Joined</asp:ListItem>
</asp:DropDownList>
</div>
<div>
<asp:Label runat="server" ID="searchLabel" Text="Search For: " />
<asp:TextBox ID="searchTextBox" runat="server" Columns="30" />
<asp:Button ID="searchButton" runat="server" Text="Search" />
</div>
<div>
<asp:UpdatePanel ID = "up" runat="server">
<ContentTemplate>
<div style= "overflow:auto; height:150px; width:700px">
<asp:GridView ID="DefaultGrid" runat = "server" DataKeyNames = "fName, lName, zip"
onselectedindexchanged = "DefaultGrid_SelectedIndexChanged"
autogenerateselectbutton = "true"
selectedindex="0">
<SelectedRowStyle BackColor="Azure"
forecolor="Black"
font-bold="true" />
<Columns>
<asp:TemplateField HeaderText="Processed">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxProcess" OnCheckedChanged="CheckBoxProcess_CheckedChanged" runat="server" Enabled="true" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
<asp:TextBox ID="OrderBrowser" columns="70" Rows="14" runat="server" Wrap="false" TextMode="MultiLine" ReadOnly = "true"/>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
【问题讨论】:
【参考方案1】:这是因为当您单击复选框时,在 AJAX 回发期间,您的 page_load
事件会在您的 Gridview 再次绑定的 CheckBoxProcess_CheckedChanged
之前触发。
应该是……
If(!IsPostBack) // you missed this condition
orderByString = orderByList.SelectedItem.Value;
fieldString = searchTextBox.Text;
string sqlStatement = "SELECT fName,lName,zip,email,cwaSource,price,length FROM SecureOrders WHERE fName LIKE '%" + fieldString + "%' OR lName LIKE'%" + fieldString + "%' OR zip LIKE'%" + fieldString + "%' OR zip LIKE'%" + fieldString + "%' OR email LIKE'%" + fieldString + "%' OR cwaSource LIKE'%" + fieldString + "%' OR length LIKE'%" + fieldString + "%' OR price LIKE'%" + fieldString + "%' ORDER BY " + orderByString;
////////////////////////////
System.Configuration.Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager.
OpenWebConfiguration("/Cabot3");
System.Configuration.ConnectionStringSettings connectionString;
connectionString = rootWebConfig.ConnectionStrings.ConnectionStrings["secureodb"];
//TEST
for (int i = 0; i < DefaultGrid.Rows.Count; i++)
CheckBox chkUpdate = (CheckBox)DefaultGrid.Rows[i].Cells[1].FindControl("CheckBoxProcess");
if (chkUpdate != null)
OrderBrowser.Text += "Test";
// Create an SqlConnection to the database.
using (SqlConnection connection = new SqlConnection(connectionString.ToString()))
connection.Open();
SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlStatement, connection);
// create an SqlCommandBuilder - this will automatically generate the
// commands, and set the appropriate properties in the dataAdapter
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
// create the DataSet
DataSet dataSet = new DataSet();
// fill the DataSet using our DataAdapter
dataAdapter.Fill(dataSet, "SecureOrders");
SqlCommand cmd = new SqlCommand("SELECT * FROM SecureOrders", connection); // might not need this
SqlCommand bitCmd = new SqlCommand("Select IsNull(processed,0) as processedField From SecureOrders", connection);
DataView source = new DataView(dataSet.Tables[0]);
DefaultGrid.DataSource = source;
DefaultGrid.DataBind();
编辑: 关注您的 cmets 后,您无需迭代 gridiew 行来设置复选框状态。您可以使用 GridView 的 RowDataBound 事件或直接绑定该值。例如
<asp:CheckBox ID="CheckBoxProcess" Checked='<%#Eval("processedField") %>' OnCheckedChanged="CheckBoxProcess_CheckedChanged" runat="server" Enabled="true" />
更新:请用此代码更改以下代码
// Create an SqlConnection to the database.
using (SqlConnection connection = new SqlConnection(connectionString.ToString()))
connection.Open();
SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT * FROM SecureOrders", connection);
// create the DataSet
DataSet dataSet = new DataSet();
// fill the DataSet using our DataAdapter
dataAdapter.Fill(dataSet, "SecureOrders");
DataView source = new DataView(dataSet.Tables[0]);
DefaultGrid.DataSource = source;
DefaultGrid.DataBind();
【讨论】:
这看起来很棒 - 唯一的问题是数据实际上并未绑定到此处的复选框 - 如何将一个字段绑定到 gridview 中的一列? 您以错误的方式绑定复选框值,您不需要迭代当前正在执行的所有行。您可以使用 RowDataBound 事件或直接绑定复选框值。检查我的答案更新。 好的,我已经尝试过了,但我一生都无法弄清楚我需要在“ColumnName”中添加什么。我尝试使用表中的名称,但它无法识别它! 你在数据库中的字段名称是什么? 然后用这个设置复选框 Checked=''以上是关于页面加载时,显示选中或未选中的复选框的主要内容,如果未能解决你的问题,请参考以下文章
如何启用在页面加载时选中的第一个复选框并仅显示数据目标 div 并隐藏其他复选框,并且一次仅选中一个复选框