GridView when the page is refreshed - asp.net

I have a GridView and a Button in a web form.
I change SelectCommand of SqlDataSource when user click on the button.
Then the result has been shown in GridView. But when the page is refreshed, results changes. For example when user wants to change the page of GridView.
Why?

Seems that your data is bounded again to grid for every Page load.
Please make sure that the the setting the datasource and databinding is not done on every page load..
Put your code in (!IsPostBack) like this:
if(!IsPostBack)
{
Gridview1.DataSource=yourDataSource;
GridView1.DataBind();
}

Related

Navigate to a certain page of a GridView from an other aspx page

I have a website with 2 pages, Master.aspx and Detail.aspx.
In Master.aspx there is a Gridview and when I click on a certain row it will redirect to Detail.aspx.
The Gridview inside Master.aspx implements "pager size", so when the user is inside Detail.aspx, I want them to be able to return to Master.aspx, precisely in the same page Gridview. To solve this problem I'm using the following JavaScript:
history.go(-n)
This allows the user to return to the correct Gridview page, however, it doesn't update the information inside the Gridview (naturally when the user makes changes in Detail.aspx).
How can I update the information inside the Gridview appropriately?
Moreover, I noticed that when I click the page buttons of the Gridview they use a __doPostBack() function:
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$GridView1','Page$3')"
style="color:White;">3</a>
Is it possible to use the __doPostBack function from Detail.aspx, to redirect to a specific page of the Gridview in Master.aspx?
When redirecting back to Master.aspx from Default.aspx you can have it go to the correct page index by passing the page index number through the url
Response.Redirect("Detail.aspx?id=" + gridview1.PageIndex.ToString()).
On Master.aspx you would then have to set the content of gridview1 then do
gridview1.DataBind();
gridview1.PageIndex = Request.QueryString("id")

Pass datatable to different page to be diaplyed in gridview

I have an aspx page with a gridview control. The code-behind for this page fills a datatable and then displays the datatable in the gridview. I then need to pass the datatable to a separate aspx page and display it again in a gridview on THAT page. I'm not sure how to go about this.
On the second page, in the code-behind, I'll have a simple function that receives a datatable and displays it in a gridview, but what is the best way to do what I'm trying to do? I need the gridview to display on the original page, but also display on the new page.
So... somehow display the gridview on page1, then pop open the new page and pass the datatable to my function? At a loss here.
You can try using Sessions, once you display the GridView for the first time, just save the DataTable it loaded into a Session, something like Session["griddata"] = myDataTable. Then in the other page just pull it out of the Session likeDataTable myDataTable = (DataTable)Session["griddata"]; then bind it to the grid.

How can I pevent Gridview.DataBind when a commandField is pressed

I have a gridview included "Edit CommandFields Update and Cancel" at the last column of my gridview. Also, I have a search button above my Gridview and everything works properly together. When I am searching for a particular Column the "sqlDataSource" of my Gridview is changing respectively on the query and displays the results.
However, the Edit/Update/Cancel command field buttons refresh the whole Gridview and I am loosing the selected editable row derived from the search results.
How can I prevent grdiview Databing when I click the Edit Command Field ?
Any advice would be appreciated,
You are probably missing the famous is Page.IsPostback check.In vb.net you will have to do some thing like this
If Not Page.IsPostBack
->your binding code goes here
End if
C#
if (!Page.IsPostBack)
{
->your binding code goes here
}
This way your results are not lost when the page reloads.

two different user controls in same asp.net page

this is my application link please download
i had 2 different user controls in one .aspx page.
first user control was dropdownlist binded with database.
second user control was gridview.
if i select a company int the dropdownlist products of that company should be displayed in the second user control.
my problem was, when i am selecting the companies in the dropdownlist gridview user control is loading first and 2nd dropdownlist selected event is firing..
please help me...
You should probably check the IsPostBack property and only populate the controls if it isn't a postback.
void Page_Load()
{
if (!IsPostBack)
{
// Populate the user controls.
}
}
But since you didn't show any code I'm just guessing.

How do I add a textbox dynamically in ASP.NET?

I've a following requirement for my asp.net page:
User can add a textbox dynamically on a page A by clicking on link "Add a new category" hyperlink
He clicks submit button on page A and gets redirected to page B.
When he clicks on page A link from this page, the textboxes that he added should be persisted.
Can someone help me with the code on this?
Appreciate your help!
In the ButtonClick Method write.
TextBox tb = new TextBox();
Parent.Controls.Add( tb );
The Parent is the control you want to add the textbox to, for instance a panel.
You can also look at this resource.
Hope it helps.
Adding a user control dynamically is simple. But in this case, I don't think you need to do that, instead you should look at creating a repeater with a textbox inside it, and when the user clicks Add Category, add one item to the repeater datasource.
This way you can handle both control creation and state persistence at the same time.
dealing with dynamic user controls can be a pain in the ass.
as a rule of thumb i follow, whenever you create a dynamic user control, then you must set it's ID so ASP.net can reallocate it on post back, and to keep the controls values after post back you should reload your user controls on Page_Init event.
hope this helps.
Dynamically creating Textboxes:
suppose you have page like this
when you enter '1' in textbox and click on ADD button,output will be like display one textbox
below..
i have designed like this,
i have one textbox and placeholder for displaying dynamic textboxes..
double click on Add button...in btnadd_click,you have to write the following code
protected void btnadd_Click(object sender, EventArgs e)
{
int i;
for (i = 0; i < Convert.ToInt32(txtno.Text); i++)
{
TextBox txtbox = new TextBox();
phtxt.Controls.Add(txtbox);
phtxt.Controls.Add(new LiteralControl("<br>"));
}
}
debug it... and the output is,
As others have stated adding the text box dynamically is fairly straight forward, just create the Textbox and add it to the controls collections wherever you need it to show up. You then need to store the information that this user gets this additional text box. Assuming that this is meant for long term, you will need to store this information in your backend store. Whenever you are constructing the page you will need to read the store information first to see what textboxes to create.
I would suggest doing it as follows. In the Onload event, if you have not done so before, load the dynamic information from your DB. Add any necessary controls to the page and store this information in viewstate. On any subsequent postbacks, read the information from viewstate to add the additional controls. This will save you from having to read constantly from the database on each postback.

Resources