Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

in this, the sitename must redirect to next tab(ie, when i click on sitename it will open in another page )
plz help me
Posted
Updated 14-Mar-13 22:42pm
v2
Comments
frostcox 15-Mar-13 4:36am    
Hey you need to set the DataSource of the GridView to the DataTable you return.

Then in the RowDataBound event of the gridview set the PostBackUrl of the desired column using an anchor tag. If you need any more help i'm happy to help
manjujasmine 16-Mar-13 0:52am    
thank u for ur rply


XML
<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>");
}

}
 
Share this answer
 
v2
Comments
manjujasmine 16-Mar-13 0:04am    
ITS RLY HELPFULL THANKS
manjujasmine 25-Mar-13 2:45am    
Hi,
These codes are rly Helpful and working but now i want to get its ip address to the address bar.
(ipaddress taken from my sql db)
HI,

bind the Gridview normally, as every body does. Make the link button html as below.
ASP.NET
<asp:linkbutton id="YourID" runat="server" autopostback="false" text='<%#Eval("URL")%>' onclientclick="return OpenNewTab(this)" />

and use the javascript function like below.
JavaScript
function OpenNewTab(sender)
{
    if(sender.innerText !="")
    {
        return window.open(sender.innerText,'_blank');
    }
    else
    {
        return false;
    }
}


hope it helps
 
Share this answer
 
v3
Comments
manjujasmine 16-Mar-13 0:04am    
THANKS FOR UR REPLY
Hi,

Have a data access layer as a class file to actually differentiate between your presentation class and your code logics.

Create a method in your class file as below:

C#
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;
}


then in your aspc.cs file, you might want to populate your grid on Page_Load(),

C#
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();
}


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
 
Share this answer
 
Comments
manjujasmine 16-Mar-13 0:05am    
THANKS FOR UR REPLY

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900