Showing posts with label gridview. Show all posts
Showing posts with label gridview. Show all posts

Wednesday, March 28, 2012

Why IsPostBack=false? How to repair that?

I'm using Ajax.Net. There is UpdatePanel on the page. It contains GridView. When I press "Edit", "Update" or "Cancel" button in the GridView then UpdatePanel reloads. Also executes code "if (!IsPostBack) { ... }" in Page_Load event on the server - but I don't need it!!! This code must run only once when page is loading for the first time.

protected void Page_Load(object sender, EventArgs e) {if (!IsPostBack) { ...// Here there are bindings for each control. // This code must run only once ...// On every action with GridView (which is placed in the UpdatePanel) // this code executes again and again. // Why? And how to prevent that? } }

1) Why IsPostBack=false (in Page_Load) on any action with GridView?

2) How to prevent that?

when you are clicking the button it's like you are reloading the all page but not the design... If you do not bind again the gridview loses data... Best solution is to cache the datatable that binds the gridview and binding the gridview to that... You can always verify the state of the grid to verify if you do some code...

As I understood, on every asynchronious postback (or mayme it's callback) the page is creating as it is not postback (isPostBack=false).

I think that it is better to use Session instead of Cache because every user has it's own data in the tabe.


cache in client... it's not postback... because the reference it's different... when you have difficulties test without the updatepanel to see if it's another reference of the page or not...

and i know what you're thinking the adress it's the same... it's ASP my friend... works that way.


What means "cache in client"? How? I can cache data only on server.


HttpContext.Current.Cache.Insert(itemString, Obj, HttpCacheability.Public, DateTime.Now.AddHours(expiration), TimeSpan.Zero)



Oh, it's caching on the server side, I thought you means caching data on the client side.

I change a bit my question. So, on each asynchronious postback executes code "if (!IsPostBack) { ... }" in Page_Load. But I want to prevent it. This code must executes only once when page is loading for the first time. How can I know if the page is loading for the first time or it is asynchronious postback? I want something like that: "if (!IsPostBack && !isAsynchroniousPostBack) { ... }"


did you tried with page.callback?


most likely no. what do you mean?


Put if not ispostback and not iscallback...


I wrote "!IsPostBack && !IsCallback && !IsAsync" - IsCallback and IsAsync always =false. IsPostBack in some cases =true and in some cases = false


tell me what are the cases that give true...

you don't have the need to put all of this...

if it doesn't control for you the postback you can put a viewstate that keeps the state of the grid.

just like this:

If not ispostback and viewstate("Should_Enter_Here")

end if

and in the event of the grid put this viewstate equals a true.


What you are looking for isIf (!ScriptManager.GetCurrent(Page).IsInAsyncPostBack).

http://www.asp.net/learn/ajax-videos/video-173.aspx


Thanks

why Javascript doesnt work properly in updatepanel?

i wanna my gridview to change into editrow model by click any space in the row , so i wrote in the aspx page:

.....
<script type="text/javascript">
function ClickEvent(cId)
{
var id=cId;
document.getElementById("rowid").value=id;
document.getElementById("btnBindData").click();
}
</script>
.....

<asp:LinkButton ID="btnBindData" runat="server" OnClick="btnBindData_Click" Width="0px" style="width:0px; height:0px"></asp:LinkButton>
<input type="hidden" id="rowid" runat="server" /> //send clicked row index to editindex property

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="userName" HeaderText="userName" SortExpression="userName" />
<asp:BoundField DataField="userPassword" HeaderText="userPassword" SortExpression="userPassword" />
</Columns>
</asp:GridView>

In the aspx.cs file i wrote:

.......

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{

e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#dfe345'");

e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

e.Row.Attributes["style"] = "Cursor:hand";

e.Row.Attributes.Add("OnClick", "ClickEvent('" + e.Row.RowIndex + "')");

}
}

......

protected void btnBindData_Click(object sender, EventArgs e)
{
GridView1.EditIndex=Convert.Int32(this.rowid.Value);

.....

GridView1.DataBind();
}

It works well without UpdatePanel ,but when put the GridView in the UpdatePanel ,when I click the row, the GridView got disappeared.

Replace asp:LinkButton with asp:Button. If you want you can sorround your button with a hidden div (to hide from the user). Also add UseSubmitBehavior="false" to the asp:Button.

For example:

<div style="display: none; line-height: 0pt;">
<asp:Button id="btnBindData" runat="server" OnClick="btnBindData_Click" UseSubmitBehavior="false" />
</div>