且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将sql表中的值绑定到网格视图

更新时间:2022-06-02 02:56:42


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

}


HI,



绑定Gridview通常就像每个人一样。将链接按钮html设置如下。



bind the Gridview normally, as every body does. Make the link button html as below.
<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






将数据访问层作为类文件实际区分您的表示类和代码逻辑。



在类文件中创建一个方法,如下所示:



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:

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