我有一个gridview,我正在动态添加两个按钮“编辑和删除”(通过代码)。这是我的 HTML 代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="4"
BorderWidth="1px" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#990000" ForeColor="White" Font-Bold="True" />
<HeaderStyle Height="30px" BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#333333" HorizontalAlign="Center" BackColor="#E2E2E2" />
<RowStyle CssClass="test" BackColor="#E2E2E2" Height="25px" BorderWidth="1px" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2E2E2" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
这是我的代码背后的代码::
private void BindData(DataTable dtColumns)
{
GridView1.Columns.Clear();
String Query = "";
DataTable dtSearchItems = (DataTable)ViewState["dtSearchItems"];
String PrimaryKey = "";
if (ViewState["PrimaryKey"] != null)
{
PrimaryKey = ViewState["PrimaryKey"].ToString();
}
if (dtColumns != null)
{
String Columns = "";
if (dtColumns.Rows.Count > 0)
{
foreach (DataRow dr in dtColumns.Rows)
{
Columns += dr["OrignalColumn"].ToString() + ",";
}
Columns = Columns.TrimEnd(',');
Query = "Select " + PrimaryKey + " , " + Columns + " from " + TableName + " where 1 = 1 ";
}
else
{
Query = "Select *," + PrimaryKey + " from " + TableName + " where 1 = 1 ";
}
}
else
{
Query = "Select *," + PrimaryKey + " from " + TableName + " where 1 = 1 ";
}
if (dtSearchItems != null)
{
if (dtSearchItems.Rows.Count > 0)
{
foreach (DataRow dr in dtSearchItems.Rows)
{
Query += " " + dr["AndOr"].ToString() + " " + dr["ColumnName"].ToString() + " " + ClsStaticSearch.ReturnOperationSymbol(dr["Operation"].ToString(), dr["Value"].ToString());
}
}
else
{
Query = Query;
}
}
DataTable dtOutPutResult = new DataTable();
SqlConnection con = new SqlConnection();
con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
SqlCommand cmd = new SqlCommand(Query, con);
if (con.State == ConnectionState.Closed) con.Open();
dtOutPutResult.Load(cmd.ExecuteReader());
GridView1.DataKeyNames = new string[] { PrimaryKey };
if (dtOutPutResult.Rows.Count > 0)
{
foreach (DataColumn dcDtOutPutResult in dtOutPutResult.Columns)
{
foreach (DataRow drDtColumns in dtColumns.Rows)
{
if (drDtColumns["OrignalColumn"].ToString() == dcDtOutPutResult.ColumnName)
{
BoundField bfield = new BoundField();
bfield.DataField = dcDtOutPutResult.ColumnName;
bfield.HeaderText = drDtColumns["DisplayColumn"].ToString();
GridView1.Columns.Add(bfield);
}
}
}
TemplateField t = new TemplateField();
DynamicTemplate mt = new DynamicTemplate(ListItemType.Item);
Button btnEdit = new Button();
btnEdit.ID = "btnEdit";
btnEdit.Visible = true;
btnEdit.OnClientClick = "return confirm_Edit();";
btnEdit.Text = " Submit";
btnEdit.CssClass = "button";
btnEdit.Style.Add("margin-right", "5px");
mt.AddControl(btnEdit, "Text", "Edit");
Button btnDelete = new Button();
btnDelete.ID = "btnDelete";
btnDelete.OnClientClick = "return confirm_delete();";
btnDelete.Visible = true;
btnDelete.Text = " Submit";
btnDelete.CssClass = "button";
mt.AddControl(btnDelete, "Text", "Delete");
t.ItemTemplate = mt;
t.HeaderText = "Activity";
GridView1.Columns.Add(t);
GridView1.DataSource = dtOutPutResult;
GridView1.DataBind();
lblMessage.Visible = false;
}
else
{
GridView1.DataSource = dtOutPutResult;
GridView1.DataBind();
lblMessage.Visible = true;
lblMessage.Style.Add("Color", "Red");
lblMessage.Text = "No Record Found.";
}
cmd.Dispose();
}
我想做的是::
我想为高级搜索创建一个通用模块,这几乎是完整的,现在我想做的是:我也想向网格添加编辑和删除功能。为此,我只需要通过 javascript 或 jquery 获取每个按钮单击的网格的 DataKey 值,因为如果页面被回发,按钮将不可见。
请帮我。