execute sqldatasource with button

浪尽此生 提交于 2020-01-16 06:45:49

问题


I have two queries that I want to be able to run on a page. I have a button, gridview and sqldatasource at the top of the page and another button, gridview and sqldatasource with the second query at the bottom.

How do I associate the buttons with their particular grid and datasource.

right now when I click either button, both gridview1 and gridview2 populate.


回答1:


I'm assuming in the markup for your grdiviews you have set the datasource property to one of the sql data sources.

I'm also going on the assumption that you do not want any data in the gridviews when the page first loads.

If that is the case you can simply leave the datasource property of the gridviews blank. Then in the click event handler for the buttons you can populate the grid view you want.

Here is the c# code I used to do this

 protected void Button2_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = SqlDataSource1;
        GridView1.DataBind();
        //If you only want to show one grid at a time
        GridView2.DataSource = null;
        GridView2.DataBind();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        GridView2.DataSource = SqlDataSource2;
        GridView2.DataBind();
        //If you only want to show one grid at a time
        GridView1.DataSource = null;
        GridView1.DataBind();
    }


来源:https://stackoverflow.com/questions/1189879/execute-sqldatasource-with-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!