按下按钮后如何数据绑定()gridview?

时间:2018-03-28 08:08:07

标签: c# asp.net gridview data-binding

我不能“为爱它”点击按钮后刷新我的GridView我知道我错过了一个数据绑定()但我不知道在哪里把它。

我在if(!isPostback)中使用page_load在页面加载时“刷新”GridView。但是我也希望在按下按钮时也这样做。

public partial class Activity : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand cmd = new SqlCommand("SELECT T1.[ActivityID] " +
                    "FROM [BI_Planning].[dbo].[tlbActivity] T1 " +
                    "ORDER BY T1.[ActivityRegisteredDate] desc", con);
                con.Open();
                gwActivity.DataSource = cmd.ExecuteReader();
                gwActivity.DataBind();
            }



    }

/*************Exec stored procedure when button is pressed******************/
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (txtActivity.Text == "")
        {
            lbSave.ForeColor = Color.Red;
            lbSave.Font.Bold = true;
            lbSave.Text = "Please enter activity!";
        }
        else

        {
            /*************Connectionstring is located in Web.config ******************/
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            /*************Exec stored Procedure from tblActivity ******************/
            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand cmd = new SqlCommand("sp_tblActivity", con);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Activity", txtActivity.Text);
                cmd.Parameters.AddWithValue("@Responsible", ddlResponsible.SelectedValue);
                cmd.Parameters.AddWithValue("@Category", ddlCategory.SelectedValue);
                cmd.Parameters.AddWithValue("@ChangeRequestor", ddlChange_Requestor.SelectedValue);
                cmd.Parameters.AddWithValue("@Priority", ddlPriority.SelectedValue);
                cmd.Parameters.AddWithValue("@Size", ddlSize.SelectedValue);
                cmd.Parameters.AddWithValue("@Status", ddlStatus.SelectedValue);
                cmd.Parameters.AddWithValue("@System", ddlSystem.SelectedValue);
                cmd.Parameters.AddWithValue("@Comment", txtComment.Text);
                con.Open();
                cmd.ExecuteNonQuery();

            }
        }
    }
}

HTML:

<%--************************ Gridview section ************************--%>
        <asp:GridView ID="gwActivity" runat="server"  AutoGenerateColumns="False" OnRowCommand="gwActivity_RowCommand" >
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkSelect" runat="server" CommandArgument='<%# Eval("ActivityID") %>'>Select</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>  
                <asp:BoundField DataField="ActivityID" HeaderText="ActivitID"></asp:BoundField>                   

            </Columns>

        </asp:GridView>

3 个答案:

答案 0 :(得分:0)

protected void Page_Load(object sender, EventArgs e)
{

}
void bind()
{
 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ToString());
           con.Open();
           SqlCommand cmd = new SqlCommand("SELECT T1.[ActivityID], T1.[Activity], T1.[ActivityRegisteredDate], T2.[Responsible], T3.[Category], T4.[Change_Requestor], T5.[Priority], T6.[Size], T7.[Status], T8.[System], T1.[Comment]" +
                "FROM [BI_Planning].[dbo].[tlbActivity] T1 " +
                "LEFT JOIN [BI_Planning].[dbo].[tblBI_Responsible] T2 ON T1.BI_ResponsibleID = T2.BI_ResponsibleID " +
                "LEFT JOIN [BI_Planning].[dbo].[tblCategory] T3 ON T1.CategoryID = T3.CategoryID " +
                "LEFT JOIN [BI_Planning].[dbo].[tblChange_Requestor] T4 ON T1.[ChangeRequestorID] = T4.[ChangeRequestorID] " +
                "LEFT JOIN [BI_Planning].[dbo].[tblPriority] T5 ON T1.[PriorityID] = T5.[PriorityID] " +
                "LEFT JOIN[BI_Planning].[dbo].[tblSize] T6 ON T1.[SizeID] = T6.[SizeID] " +
                "LEFT JOIN [BI_Planning].[dbo].[tblStatus] T7 ON T1.[StatusID] = T7.[StatusID] " +
                "LEFT JOIN [BI_Planning].[dbo].[tblSystem] T8 ON T1.[SystemID] = T8.[SystemID] " +
                "ORDER BY T1.[ActivityRegisteredDate] desc", con);

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        if (dt.Rows.Count != 0)
        {
            gwActivity.DataSource = dt;
            gwActivity.DataBind();
           }
         else
          {
          // For Empty Gridview
          DataTable dt1 = new DataTable(); 
         //Add What you want 
            dt1.Columns.Add(new DataColumn("city_name", typeof(string)));
           dt1.Rows.Add(dt1.NewRow());
            gwActivity.DataSource = dt1;
            gwActivity.DataBind();
            }}

按钮

protected void Button1_Click(object sender, EventArgs e)
{
    if (txtActivity.Text == "")
    {
        lbSave.ForeColor = Color.Red;
        lbSave.Font.Bold = true;
        lbSave.Text = "Please enter activity!";
    }
    else

    {

       bind();

    }
    }
}

}

答案 1 :(得分:0)

首先,我将使用您的代码,但我建议从DAL划分UI。

您可以调用GrdBind函数:

public partial class Activity : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
            GrdBind();
       }
   }

   private void GrdBind()
   {
        string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
        using (SqlConnection con = new SqlConnection(CS))
        {
            SqlCommand cmd = new SqlCommand("SELECT T1.[ActivityID], T1.[Activity], T1.[ActivityRegisteredDate], T2.[Responsible], T3.[Category], T4.[Change_Requestor], T5.[Priority], T6.[Size], T7.[Status], T8.[System], T1.[Comment]" +
                "FROM [BI_Planning].[dbo].[tlbActivity] T1 " +
                "LEFT JOIN [BI_Planning].[dbo].[tblBI_Responsible] T2 ON T1.BI_ResponsibleID = T2.BI_ResponsibleID " +
                "LEFT JOIN [BI_Planning].[dbo].[tblCategory] T3 ON T1.CategoryID = T3.CategoryID " +
                "LEFT JOIN [BI_Planning].[dbo].[tblChange_Requestor] T4 ON T1.[ChangeRequestorID] = T4.[ChangeRequestorID] " +
                "LEFT JOIN [BI_Planning].[dbo].[tblPriority] T5 ON T1.[PriorityID] = T5.[PriorityID] " +
                "LEFT JOIN[BI_Planning].[dbo].[tblSize] T6 ON T1.[SizeID] = T6.[SizeID] " +
                "LEFT JOIN [BI_Planning].[dbo].[tblStatus] T7 ON T1.[StatusID] = T7.[StatusID] " +
                "LEFT JOIN [BI_Planning].[dbo].[tblSystem] T8 ON T1.[SystemID] = T8.[SystemID] " +
                "ORDER BY T1.[ActivityRegisteredDate] desc", con);
            con.Open();
            gwActivity.DataSource = cmd.ExecuteReader();
            gwActivity.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        GrdBind();
    }
}

答案 2 :(得分:0)

您可以将Gridview绑定代码放入一个方法中,并可以同时调用Page_LoadButton Click个事件。

public void LoadData()
{
       string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
        using (SqlConnection con = new SqlConnection(CS))
        {
            SqlCommand cmd = new SqlCommand("SELECT T1.[ActivityID], T1.[Activity], T1.[ActivityRegisteredDate], T2.[Responsible], T3.[Category], T4.[Change_Requestor], T5.[Priority], T6.[Size], T7.[Status], T8.[System], T1.[Comment]" +
                "FROM [BI_Planning].[dbo].[tlbActivity] T1 " +
                "LEFT JOIN [BI_Planning].[dbo].[tblBI_Responsible] T2 ON T1.BI_ResponsibleID = T2.BI_ResponsibleID " +
                "LEFT JOIN [BI_Planning].[dbo].[tblCategory] T3 ON T1.CategoryID = T3.CategoryID " +
                "LEFT JOIN [BI_Planning].[dbo].[tblChange_Requestor] T4 ON T1.[ChangeRequestorID] = T4.[ChangeRequestorID] " +
                "LEFT JOIN [BI_Planning].[dbo].[tblPriority] T5 ON T1.[PriorityID] = T5.[PriorityID] " +
                "LEFT JOIN[BI_Planning].[dbo].[tblSize] T6 ON T1.[SizeID] = T6.[SizeID] " +
                "LEFT JOIN [BI_Planning].[dbo].[tblStatus] T7 ON T1.[StatusID] = T7.[StatusID] " +
                "LEFT JOIN [BI_Planning].[dbo].[tblSystem] T8 ON T1.[SystemID] = T8.[SystemID] " +
                "ORDER BY T1.[ActivityRegisteredDate] desc", con);
            con.Open();
            gwActivity.DataSource = cmd.ExecuteReader();
            gwActivity.DataBind();
        }
}

在Page_load和Button_Click事件中调用此方法。