问题描述
我创建一个像
T1的表
----
i create a table like
T1
----
id sitename portid createdate
--- -------------------- --------- --------------
1 www.google.com 8050 2012-11-10
现在我想在此表格中将网站名称显示为网格视图,例如
now i want to display sitename to a gridview from this table like
slno sitename
----- -----------
1 www.google.com
在此,网站名称必须重定向到下一个标签(即,当我点击网站名称时,它将在另一个页面中打开)
plz帮助我
in this, the sitename must redirect to next tab(ie, when i click on sitename it will open in another page )
plz help me
推荐答案
<asp:GridView ID="grdview" runat="server" AutoGenerateColumns="false" OnRowCommand="grdview_RowCommand">
<Columns>
<asp:BoundField ReadOnly="True" HeaderText="sno" InsertVisible="False" DataField="sno"
SortExpression="sno"></asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CommandArgument='<%#Eval("site")%>'
Text='<%#Eval("site")%>' CommandName='site' CausesValidation="False"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected
ASPX .CS
void Page_Load(object sender,EventArgs e)
{
if(!IsPostBack)
{
con.Open();
SqlCommand cmd = new SqlCommand( select * from newtable,con);
SqlDataAdapter dtAdapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
dtAdapter.Fill(ds);
con.Close();
grdview.DataSource = ds.Tables [0];
grdview。 DataBind();
}
}
protected void grdview_RowCommand(object sender,GridViewCommandEventArgs e)
{
if(e.CommandName ==site)
{
string url =http://+ e.CommandArgument.ToString ();
Response.Write(< script> window.open(''+ url +'');< / script>);
}
}
protected
ASPX.CS
void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from newtable", con);
SqlDataAdapter dtAdapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
dtAdapter.Fill(ds);
con.Close();
grdview.DataSource = ds.Tables[0];
grdview.DataBind();
}
}
protected void grdview_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "site")
{
string url = "http://" + e.CommandArgument.ToString();
Response.Write("<script>window.open(''" + url + "'' );</script>");
}
}
<asp:linkbutton id="YourID" runat="server" autopostback="false" text='<%#Eval("URL")%>' onclientclick="return OpenNewTab(this)" />
并使用如下的javascript函数。
and use the javascript function like below.
function OpenNewTab(sender)
{
if(sender.innerText !="")
{
return window.open(sender.innerText,'_blank');
}
else
{
return false;
}
}
希望有帮助
hope it helps
public DataTable PopulateGrid()
{
//Have a connection object here, name it as xconn...
SqlDataAdapter da=new SqlDataAdapter("select id as slNo,sitename from dbo.Table",xconn);
DataTable dt=new DataTable();
da.Fill(dt);
return dt;
}
然后在你的aspc.cs文件中,你可能想要在Page_Load()上填充网格,
then in your aspc.cs file, you might want to populate your grid on Page_Load(),
protected void Page_Load()
{
//create an object of your data access layer
DAL obj=new DAL();
DataTable dt=obj.PopulateGrid();
gridview.DataSource=dt;
gridView.DataBind();
}
使用上述代码,您可以在Page_Load()上填充网格。
当您单击gridview时重定向到站点,您实际上可以设置所需列的PostBackUrl属性,它将起到作用。
希望它有帮助。
-Anurag
Using the above codes you will be able to populate your grid on Page_Load().
For redirecting to the site when you click your gridview, you can actually set the PostBackUrl property of the required column, and it will do the trick.
Hope it helps.
-Anurag
这篇关于如何将sql表中的值绑定到网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!