【问题标题】:How to Update GridView after PostBack occurred by a control outside GridView?GridView 外部的控件发生 PostBack 后如何更新 GridView?
【发布时间】:2014-03-13 14:52:15
【问题描述】:

我有一个GridView,当用户单击此GridView 之外的按钮时将更新它,应为特定行更新GridView 列。 所以我在PostBack 中为GridView 出价新数据。但正如我们所知,PostBackOnClick 按钮事件之前被调用。所以GridView此时是绑定的。但我希望根据OnClick 按钮事件中的一些操作更新特定行的GridView 列值。 所以我也尝试在OnClick 按钮事件中绑定GridView。但它没有得到更新。 所以我的主要疑问是。

这是否可以在导致PostBack 的点击事件中在PostBack 块中调用的方法中传递值?

private void bindTheGriView()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("Row Number", typeof(string)));
        dt.Columns.Add(new DataColumn("POS Id", typeof(string)));
        dt.Columns.Add(new DataColumn("Action", typeof(string)));
        dt.Columns.Add(new DataColumn("Status", typeof(string)));
        for (int index = 0; index < m_listStrPendingListOfPOS.Count; index++)
        {
            dr = dt.NewRow();
            int iRowNo = index + 1;
            dr["Row Number"] = iRowNo;
            string strGridViewPOSId = m_listStrPendingListOfPOS[index];
            dr["POS Id"] = strGridViewPOSId;
            dr["Action"] = string.Empty;
            //check for the flag. if the flag is true set status to Pending else to Associated
            dr["Status"]=((Label)GridViewMultiplePOSAssociationId.Rows[index].FindControl("LabelStatusPendingPOSId")).Text;
           dt.Rows.Add(dr);
        }

        ViewState["POSTable"] = dt;
        GridViewMultiplePOSAssociationId.DataSource = dt;
        GridViewMultiplePOSAssociationId.DataBind();
    }


    protected void btnSave_Click(object sender, EventArgs e)
{
    bool statusFlag=false;
    if (ViewState["RowIndexPOS"] != null)
    {
        int iRowIndex = Convert.ToInt32(ViewState["RowIndexPOS"]);
        Label lblStatus = (Label)GridViewMultiplePOSAssociationId.Rows[iRowIndex].FindControl("LabelStatusPendingPOSId");
        //Means all rows in GridView are successfully associated
        if (table.Rows.Count == iResultCount)
        {
            lblStatus.Text = "Associated";
        }
        else
        {
            lblStatus.Text = "Pending";
        }
    }
    //now call the binding method with the bool flag value
     bindTheGriView();
}

不,我没有使用更新面板。

【问题讨论】:

  • 你能展示一些gridview和按钮点击事件的代码吗?
  • 发布一些代码并告诉我们您是否可以使用 UpdatePanels。答案将取决于 UpdatePanels 是否是一个选项。
  • 修改了问题。

标签: c# asp.net gridview postback


【解决方案1】:

不要在回发中这样做。将您的数据绑定抽象为一个方法,并从相应的事件中调用该方法。

public void BindMyData()
{
    // Do data bindings on all bound controls
}

public void Page_Load(object sender, EventArgs e) 
{
    if (!IsPostBack)
        BindMyData();
}

public void myClick(object sender, EventArgs e)
{
    // Update the data in the repository


    BindMyData();
}

或者,您可以将数据绑定移动到 Page_PreRender 事件,以确保它始终在任何控件执行后绑定。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多