在每个页面加载时避免使用dropdownlist selectedindexchanged事件 [英] Avoid dropdownlist selectedindexchanged event on every page load

查看:63
本文介绍了在每个页面加载时避免使用dropdownlist selectedindexchanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我有一个带有下拉列表的模板字段的gridview。在此下拉列表中,selectedindexchanged将填充该网格视图行。我在pageload中绑定dropdownlist项目。我不能把它放进去!IsPostBack因为我使用web用户控件需要在每个页面加载时绑定。单击保存按钮下拉列表时,seletedindexchanged事件将触发gridview中的每一行,并且gridviewrow将丢失所做的任何更改。你能告诉我怎样才能避免触发dropdownlist selectedindex改变事件。它只需要在下拉列表的selectedindexchanged上触发。



谢谢

Hello,

I have a gridview with a template field having dropdownlist. On selectedindexchanged of this dropdownlist data will be populated in that grid view row. I am binding dropdownlist items in pageload. I cant put it in !IsPostBack because i am using web user controls need to bind on every page load. On click of save button dropdownlist seletedindexchanged event is firing for every row in the gridview and gridviewrow loosing any changes made. Can you please tell me how can i avoid firing dropdownlist selectedindex changed event. It need to be fired on only selectedindexchanged of dropdownlist.

Thanks

推荐答案

不,你必须把GridView Bind函数放在!IsPostBack 中,否则会出现很多问题,包括你指定的问题。



这是因为,在每个事件触发之前,它将首先进入页面加载。如果你在PageLoad上绑定GridView,那么它将再次重新加载。



因此,当事件将在此之后被触发时,它将显示旧的Grid已加载旧值。



因此,请将代码放入!IsPostBack



现在根据您的要求,您需要有时刷新网格。

为此,只需在事件或函数内调用Bind函数你想绑定它。如下所示。

No, you have to put the GridView Bind function inside the !IsPostBack, otherwise there will be many problems including the problem you have specified.

This is because, before every event fires, it will go to page load first. And if you are binding the GridView on PageLoad, then it will be newly loaded again.

So, when the event will be fired after that, it will show the old values as the Grid is already loaded with old values.

So, put your code in !IsPostBack.

Now as per your requirement, you need to refresh the Grid sometimes.
For that, just call the Bind function inside the event or function you want to bind it. Like below.
protected void Button1_Click(Object sender, EventArgs e)
{
    // Call Bind function here. It will refresh the Grid if you need it. 
    // Don't call Bind function in every PageLoad. Always check !IsPostBack.
    BindGrid();
}


还可以尝试一下。您可以检查是否由于Save按钮发生回发,如果是,则不要进行数据绑定。



以下条件检查是否由于保存按钮点击而发生回发。





One more thing you can try. You can check whether postback happen due to Save button click if yes than don''t do data binding.

Below condition check whether postback happen due to save button click.


bool bindData = true;
        if (Request[btnSave.UniqueID] != null)
        {
            bindData = false;
        }







你可以在绑定数据的任何地方使用bindData标志。




you can bindData flag wherever your binding data.


这篇关于在每个页面加载时避免使用dropdownlist selectedindexchanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆