我的web应用程序中有一个动态生成的html表。其中一列列出了asp复选框控件。我想选择多行(通过checkboxex)来发布,取消发布或删除我的文章。它成功发布或取消发布asp按钮的第一个回发。但是,当我第二次点击时,它会为所有复选框发送false,甚至其中一些被选中。所以我希望它必须适用于第二次回发,如果它第一次工作,因为它已成功通过了所有的页面生命周期第一次然后为什么不第二次..我真的很困惑。请帮助我 这是我的代码:
//对于我在oninit状态下使用的动态生成的控件
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (!this.DesignMode)
{
ds = GetArticles();
FillArticleTable();
}
}
//页面加载的代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["id"] != null)
{
string eventdata = Request.QueryString["event"];
int id = Convert.ToInt32(Request.QueryString["id"]);
if (eventdata != "edt")
{
Actions(id, eventdata);
}
else
{
Response.Redirect("Articles.aspx?id=" + id + "");
}
}
else
{
ds = GetArticles();
Cache["Articles"] = ds;
}
}
}
// Button Click事件的代码
protected void btnApplyAction_Click(object sender, EventArgs e)
{
if (this.ddlActions.SelectedValue != "none")
{
IterateOverHtmlTable(this.ddlActions.SelectedValue);
this.myTable.Rows.Clear();
ds = GetArticles();
FillArticleTable();
}
}
//代码用于从数据库中检索文章
private DataSet GetArticles()
{
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection(@"Data Source = xenz-pc\sqlexpress; Initial Catalog =MedicalSystem2; User Id =sa; Password =sa;");
SqlCommand com = new SqlCommand("Select A.ID,A.ArticleTitle,A.Author,C.Category, A.Published,A.CreatedON,A.ModifiedON from Articles as A Inner Join Category As C On A.Category_ID = C.ID ", con);
con.Open();
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = com;
adp.Fill(ds);
con.Close();
return ds;
}
//动态生成html表的代码
private void FillArticleTable()
{
int rows = 0;
string imgsource;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
HtmlTableRow row = new HtmlTableRow();
rows = rows + 1;
Object[] fields = ds.Tables[0].Rows[i].ItemArray;
if (Convert.ToBoolean(fields[4]) == false)
{
imgsource = "minus-circle.gif";
}
else
{
imgsource = "tick-circle.gif";
}
for (int j = 0; j < fields.Length; j++)
{
HtmlTableCell cell = new HtmlTableCell();
if (j == 0)
{
HtmlTableCell srno = new HtmlTableCell();
srno.InnerText = rows.ToString();
cell.InnerText = fields[j].ToString();
row.Cells.Add(srno);
row.Cells.Add(cell);
}
else if (j == 1)
{
cell.InnerText = fields[j].ToString();
row.Cells.Add(cell);
}
else if (j == 2)
{
cell.InnerText = fields[j].ToString();
row.Cells.Add(cell);
}
else if (j == 3)
{
cell.InnerText = fields[j].ToString();
row.Cells.Add(cell);
}
else if (j == 4)
{
cell.InnerHtml = @"<img src=" + imgsource + " 'width=16' 'height=16' alt='published/unpublished'";
row.Cells.Add(cell);
}
else if (j == 5)
{
cell.InnerText = String.Format("{0:d}", fields[j]);
row.Cells.Add(cell);
}
else if (j == 6)
{
cell.InnerText = String.Format("{0:d}", fields[j]);
HtmlTableCell checkboxes = new HtmlTableCell();
checkboxes.Align = "Right";
HtmlTableCell actions = new HtmlTableCell();
CheckBox chkactionbox = new CheckBox();
chkactionbox.EnableViewState = true;
checkboxes.Controls.Add(chkactionbox);
actions.InnerHtml = @"<a href='ManageArticles.aspx?id=" + fields[0].ToString() + "&event=pu'><img src='tick-circle.gif' 'width=16' 'height=16' alt='published' /></a><a href='ManageArticles.aspx?id=" + fields[0].ToString() + "&event=upu'><img src='minus-circle.gif' width='16' height='16' alt='not published' /></a><a href='ManageArticles.aspx?id=" + fields[0].ToString() + "&event=edt'><img src='pencil.gif' width='16' height='16' alt='edit' /></a><a href='ManageArticles.aspx?id=" + fields[0].ToString() + "&event=del'><img src='bin.gif' width='16' height='16' alt='delete' /></a>";
row.Cells.Add(cell);
row.Cells.Add(checkboxes);
row.Cells.Add(actions);
}
}
this.myTable.Rows.Add(row);
}
}
//代码查找用户选择的复选框
private void IterateOverHtmlTable(string eventdata)
{
HtmlTable table = (HtmlTable)Page.FindControl("myTable");
int count = 0;
foreach (HtmlTableRow row in table.Rows)
{
count = count + 1;
if (count > 1)
{
foreach (CheckBox item in row.Cells[8].Controls)
{
if (item.Checked)
{
Actions(Convert.ToInt32(row.Cells[1].InnerText), eventdata);
}
}
}
}
}
//发布,取消发布和删除操作的代码
private void Actions(int id, string eventdata)
{
if (Connection != null)
{
if (Connection.State != ConnectionState.Open)
{
Connection.Open();
}
}
else
{
Connection = new SqlConnection(@"Data Source = xenz-pc\sqlexpress; Initial Catalog =MedicalSystem2; User Id =sa; Password =sa;");
if (Connection.State != ConnectionState.Open)
{
Connection.Open();
}
}
SqlCommand com = new SqlCommand();
int rows;
if (eventdata == "pu")
{
com.CommandText = "Update Articles Set Published = 'true' Where ID = " + id;
com.CommandType = CommandType.Text;
com.Connection = Connection;
rows = com.ExecuteNonQuery();
Connection.Close();
if (rows > 0)
{
this.Publish.InnerText = "Published";
}
}
else if (eventdata == "upu")
{
com.CommandText = "Update Articles Set Published = 'false' Where ID = " + id;
com.CommandType = CommandType.Text;
com.Connection = Connection;
rows = com.ExecuteNonQuery();
Connection.Close();
if (rows > 0)
{
this.Unpublish.InnerText = "Unpublished";
}
}
else
{
com.CommandText = "Delete From Articles Where ID =" + id;
com.CommandType = CommandType.Text;
com.Connection = Connection;
rows = com.ExecuteNonQuery();
Connection.Close();
if (rows > 0)
{
this.Publish.InnerText = "Deleted";
}
}
}
答案 0 :(得分:1)
我注意到有两件事可能有用: