所以我有一個表格動態生成的表單中的一個單元格中嵌套按鈕的SQL服務器。點擊按鈕應該重定向到另一個頁面。但是,當前按鈕被點擊後,頁面會刷新並顯示沒有生成表格的頁面。 我已經在按鈕在onclick上調用的方法中放置了一個斷點,並且當我單擊該按鈕時,斷點永遠不會到達。當單擊按鈕而不是重定向時,它只是刪除表
標記:
<table class="table table-bordered" runat="server">
<tr>
<td align="center">
<asp:PlaceHolder ID="dbPlaceHolder" runat="server"></asp:PlaceHolder>
</td>
</tr>
</table>
方法稱爲網頁
private void Create_Table()
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connString"].ConnectionString))
{
//Connect to databse and grab table
SqlCommand cmd = new SqlCommand("SELECT CCPOF_ID, Job_Number, Project_Name, CompletedBy, DateCreated FROM My_Table", conn);
adapter = new SqlDataAdapter(cmd);
dataTable = new DataTable();
table = new Table();
adapter.Fill(dataTable);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
TableRow row = new TableRow();
row.BackColor = ColorTranslator.FromHtml("#bd0000");
row.ForeColor = System.Drawing.Color.White;
TableHeaderCell headerCell;
string[] headerName = {"ID", "Job #", "Project", "Completed By", "Date Created", " "};
//Add Headers
for (int j = 0; j < dataTable.Columns.Count + 1; j++)
{
headerCell = new TableHeaderCell();
headerCell.Text = headerName[j];
row.Cells.Add(headerCell);
}
table.Rows.Add(row);
//Add Columns
for (int i = 0; i < dataTable.Rows.Count; i++)
{
row = new TableRow();
for (int j = 0; j < dataTable.Columns.Count+1; j++)
{
if (j < dataTable.Columns.Count)
{
TableCell cell = new TableCell();
cell.Text = dataTable.Rows[i][j].ToString();
row.Cells.Add(cell);
}
else
{
TableCell cell = new TableCell();
Button btnEdit = new Button();
btnEdit.OnClientClick = "btnEdit_Click";
btnEdit.ID = "Edit" + dataTable.Rows[i][0].ToString();
btnEdit.Attributes.Add("name", "Edit" + dataTable.Rows[i][0].ToString());
btnEdit.CssClass = "astext";
btnEdit.Text = "Edit ";
cell.Controls.Add(btnEdit);
Button btnDetail = new Button();
btnDetail.OnClientClick = "btnDetail_Click";
btnDetail.ID = "Detail" + dataTable.Rows[i][0].ToString();
btnDetail.Attributes.Add("name", "Detail" + dataTable.Rows[i][0].ToString());
btnDetail.CssClass = "astext";
btnDetail.Text = "Details";
cell.Controls.Add(btnDetail);
row.Cells.Add(cell);
}
}
table.Rows.Add(row);
}
table.CssClass = "table table-bordered";
//Add Table to Markup
dbPlaceHolder.Controls.Add(table);
}
其他方法... //方法需要被更新,但沒有關係,因爲他們從未被稱爲
protected void btnEdit_Click(object sender, EventArgs e)
{
string id = ((System.Web.UI.WebControls.Button)sender).ID;
Server.Transfer("CcpofDetails.aspx?ID=" + id);// To be changes to different page later
}
protected void btnDetail_Click(object sender, EventArgs e)
{
string id = ((System.Web.UI.WebControls.Button)sender).ID;
Server.Transfer("CcpofDetails.aspx?ID=" + id);
}
變化'btnDetail.OnClientClick = 「btnDetail_Click」;''來btnDetail.Click + =新的EventHandler(btnDetail_Click);' – techspider