静态绑定GridView我已经会了,如:
前台关键代码:
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CC9966" AllowPaging="false"
BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="100%" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"
Style="margin-top: 4px; background: #DCE3E9; border: 0px; font-size: 12px; color: #134F69;">
<HeaderStyle BackColor="#F2F9FF" />
<RowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="Apply_ID" HeaderText="编号" >
</asp:BoundField>
<asp:BoundField DataField="User_Name" HeaderText="请示人" >
</asp:BoundField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:LinkButton ID="lbtn_seek" runat="server" OnClick="lbtn_seek_Click">查询</asp:LinkButton> |
<asp:LinkButton ID="lbtn_Edit" runat="server" OnClick="lbtn_Edit_Click">编辑</asp:LinkButton</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>后台关键代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GridView1.DataKeyNames = new string[] { "Apply_ID" };
gv_bind(); //调用函数用sql查询语句绑定GridView1
}
}
由上可见GridView1有一个模板列,放置两个链接按钮,以实现跳转处理。
以下是动态绑定GridView1的关键代码:
DataTable newdt = new DataTable();
DataColumn dc1 = new DataColumn("编号", typeof(string));
DataColumn dc2 = new DataColumn("请示人", typeof(string));
newdt.Columns.Add(dc1);
newdt.Columns.Add(dc2);
DataRow dr = newdt.NewRow();
dr["编号"] = dt.Rows[0][0].ToString(); //dt是另一个DataTable,已含数据,填充过程已省略
dr["请示人"] = dt.Rows[0][1].ToString();
newdt.Rows.Add(dr);
this.GridView1.DataSource = newdt.DefaultView;
this.GridView1.DataBind();我的问题是:在绑定时怎样在GridView1中动态实现模板列,以便把原先的"查询"和"编辑"链接按钮仍放在GridView1的右边,并且点击后可以实现跳转功能?谢谢!
前台关键代码:
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CC9966" AllowPaging="false"
BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="100%" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"
Style="margin-top: 4px; background: #DCE3E9; border: 0px; font-size: 12px; color: #134F69;">
<HeaderStyle BackColor="#F2F9FF" />
<RowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="Apply_ID" HeaderText="编号" >
</asp:BoundField>
<asp:BoundField DataField="User_Name" HeaderText="请示人" >
</asp:BoundField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:LinkButton ID="lbtn_seek" runat="server" OnClick="lbtn_seek_Click">查询</asp:LinkButton> |
<asp:LinkButton ID="lbtn_Edit" runat="server" OnClick="lbtn_Edit_Click">编辑</asp:LinkButton</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>后台关键代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GridView1.DataKeyNames = new string[] { "Apply_ID" };
gv_bind(); //调用函数用sql查询语句绑定GridView1
}
}
由上可见GridView1有一个模板列,放置两个链接按钮,以实现跳转处理。
以下是动态绑定GridView1的关键代码:
DataTable newdt = new DataTable();
DataColumn dc1 = new DataColumn("编号", typeof(string));
DataColumn dc2 = new DataColumn("请示人", typeof(string));
newdt.Columns.Add(dc1);
newdt.Columns.Add(dc2);
DataRow dr = newdt.NewRow();
dr["编号"] = dt.Rows[0][0].ToString(); //dt是另一个DataTable,已含数据,填充过程已省略
dr["请示人"] = dt.Rows[0][1].ToString();
newdt.Rows.Add(dr);
this.GridView1.DataSource = newdt.DefaultView;
this.GridView1.DataBind();我的问题是:在绑定时怎样在GridView1中动态实现模板列,以便把原先的"查询"和"编辑"链接按钮仍放在GridView1的右边,并且点击后可以实现跳转功能?谢谢!
做成连接?那么把他们换成label.在OnRowCreated事件中,完成对他们赋值
你可以直接在模板列里面这样写呗
<a href='页面名称.aspx?id=<%#Eval("id")%>'>编辑</a>
<asp:GridView ID="GridView" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:HyperLinkField DataNavigateUrlFormatString="页面名称.aspx?ID={0}" DataNavigateUrlFields="id" DataTextField="name" />
</Columns>
</asp:GridView>后台:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("id"));
dt.Columns.Add(new DataColumn("name"));
dt.Rows.Add("1", "张三");
dt.Rows.Add("2", "李四"); this.GridView.DataSource = dt;
this.GridView.DataBind();
}
}
{LinkButton lbtnSearch = GridView.FindControl("lbtn_seek");
{
if (lbtnSearch != null)
{
//设置链接的URL
lbtnSearch.url = "Search.aspx?id=" + 编号;
}
}
}
以上代码仅供参考,思路是正确的,但因现在的机子没装VS,可能在书写上不是很精确。
前台代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Literal ID="ltURL" runat="server"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>后台代码:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Button1.Attributes["onclick"] = "if(confirm('确定要删除吗?')) return true; else return false;"; DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("id"));
dt.Columns.Add(new DataColumn("name"));
dt.Rows.Add("1", "张三");
dt.Rows.Add("2", "李四"); this.GridView1.DataSource = dt;
this.GridView1.DataBind();
} protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Literal ltURL = (Literal)e.Row.FindControl("ltURL");
if (ltURL != null)
{ DataRowView dv = (DataRowView)e.Row.DataItem;
ltURL.Text = string.Format("<a href ='页面名称.aspx?id={0}'>{1}</a>", dv[0], dv[1]);
}
}
}
}
在GridView1中编辑列时,添加一条新列,放入HyperLinkField,hearText设置为编辑.
在url绑定时写如下代码href='页面名称.aspx?id= <%#Eval("id")%>'