Issue Related to GridView in ASP.net - c#

I have gridview binded from database..
I have following code:
protected void Page_Load(object sender, EventArgs e)
{
BindGrid();
}
Now, I have checkBox and drop-down inside the gridView, When user selects some rows from checkboxes and clicks on Update Button, Page_Load event fires and calls BindGrid(); method and selected rows should get hidden.
How can I retain checkbox values after the page load event.
I don't want to use IsPostBack property in Page load because I have used Paging.
How can I solve my problem?

You should only DataBind the GridView if(!Page.IsPostback). Otherwise no events are triggered and ViewState values(like SelectedIndex etc.) are overwritten from the DataSource values.
http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback%28v=VS.100%29.aspx
if(!IsPostBack)
{
BindGrid();
}
You also should call BindGrid from following event-handlers:
PageIndexChanging
SelectedIndexChanged
Sorting

Make use of ISpostback..
if(!IsPostBack)
{
BindGrid();
}
call bindgrid from the pagaing event
function of paging event
{
BindGrid();
}

Related

Dropdown_load Event and Page_load?

I have to bind Drop down list when the Page load.In my previous
Project I have used Page_Load event.Now i find an event which is
Drop down_load event .I used Drop down_load and its Work well .
Is there any difference between these two events,In some cases i
have to bind more than 10 Drop down list.
so which one is best?
eg:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//bind my Dropdown
}
}
protected void ddlProduct_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//bind my Dropdown
}
}
It depends on your requirement and when you want to bind your DropDownLists. According to ASP.Net Page Life cycle Page_Load event firs first and then control events, in this case the ddlProduct_Load event.
And, this is also true according the the DropDownList Load event's definition
Fires when the page has been loaded
So, you have to make a decision where you want to bind the control.
Since you have more than 10 DropDownLists I would suggests to use their own load event to bind your control rather than making Page_Load looks too busy.

Telerik combobox - viewstate without autopostback

I turned off AutoPostback on my control because I need to validate something with javascript. And if everything is ok I'm doing postback clikcing on hidden button. The problem is that combobox looses selected value on page reloading.ViewStateMode set to Enabled. I'm populating combobox in page_load event:
protected void Page_Load(object sender, EventArgs e)
{
(!IsPostback)
{
InitializeItems(); // Helper method that binds data
}
}
Before going into the internals of Telerik, you can try to resolve the original problem. You said you want to perform validation before the postback takes place.
All you have to do is register the script you want to be ran on form submit:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
this.Page.ClientScript.RegisterOnSubmitStatement(typeof(YourClass), this.UniqueID, "your validation script here.");
}
Then in order to cancel the postback, your validation script needs to return false.

Working with dropdownList and TextBox in Asp.net c#

How do you add values to a database using a dropdownList or a TextBox without using a button in asp.net C# application?
You need to set AutoPostBack property to true and save it on index changed event.
<asp:DropDownList
ID="DropDownList1"
runat="server"
AutoPostBack="true"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
>
<asp:ListItem>option1</asp:ListItem>
<asp:ListItem>option2</asp:ListItem>
</asp:DropDownList>
protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
//save data here
}
For drop down list, its simple. Set the "AutoPostBack" property for it as "true". And handle the "SelectedIndexChanged" event like shown below.
protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
//save data here
}
But for textbox you have a server side event "TextChanged", which you can use the same way by setting "AutoPostBack" to "true" for the textbox as well.
protected void TextBox1_TextChanged(object sender, System.EventArgs e)
{
//do something here
}
The drawback with this textbox event is it gets fired with every keystroke.
So its not good for handling the database operation with every keystroke.
jQuery & an invisible button can help.
Have an asp Button, set its visibility to false. Handle its click event on the server side like shown below.
protected void Button1_Click(object sender, System.EventArgs e)
{
////save data here
}
For your text box wire up a jQuery .blur() event like shown below & fire the click event of the hidden button from it.
$("#TextBox1").blur(function() {
$("#Button1").click();
});
This would fire the button click event on server side, when you stop editing textbox value. Do your database operation from the button click event on server side.
I think you could implement it through a page load event

Gridview cell value on RowCommand

I'm attempting to get the cell value from a gridview but running into a few issues. I have setup my code similar to this example except that I'm adding my button fields on the server side. For some reason RowCommand is firing twice when clicking the cell values? TIA for any help
Page Load is empty:
code
protected void Page_Load(object sender, EventArgs e)
{
}
Adding Button Field:
code
foreach (DataColumn col in transposedTable.Columns)
{
ButtonField bfield = new ButtonField();
bfield.DataTextField = col.ColumnName;
bfield.HeaderText = col.ColumnName;
bfield.CommandName = "ColumnClick";
gvTest.Columns.Add(bfield);
}
The RowDataBound and RowCommand events are the same as the example link above
ok...There seems to be an open bug in MSFT connect...
GridView RowCommad Event Firing Twice
seems like there are some workarounds posted in the Workarounds tab...
Hope this helps...
This is a known bug. I had the same problem.
I removed the Handles GridView.RowCommand from the code behind event declaration, and
added this line to the properties declaration for the grid in the .aspx source:
OnRowCommand="GridView_RowCommand"
Worked perfectly.
One way to access row values in a grid is to use the Cells collection along with the grid's current index (which you can access via the eventargs) as shown below
void YOUR_GRID_EVENT(Object sender, GridViewDeleteEventArgs e)
{
Grid.Rows[e.RowIndex].Cells[0];
}
you can also make use of the findcontrol as below:
var txtName = e.Row.FindControl("txtName") as TextBox;
Hope this helps...
Update
Also check your code to make sure that you are calling the GridView's DataBind() method appropriately...because every time you call GridView.DataBind()...the rowcommand event of the grid view gets called...I think (guessing) you currently have a gridView.DataBind() in your onload as well as in the button click event..so that might cause the rowcommand event to be called twice...if this is not the case then post some code so that we can explore more...

How to show GridView during Page_Load?

I have a gridview that normally loads when a user clicks a View Report button. However, I now want to show the gridview while the page loads.
I tried calling the following method from the Page_Load event:
protected void btnView_Click(object sender, EventArgs e)
{
try
{
grvReport.DataBind();
}
catch (Exception ex)
{
Master.ShowMessage(ex.Message);
}
}
but it didn't work. Also tried calling grvReport.DataBind() from Page_Load to no avail.
Any suggestions?
This seems too obvious, but does the gridview have visible="true"
If Not Page.IsPostBack Then
btnView_Click(nothing,nothing)
End If
or
If Not Page.IsPostBack Then
grdNotes.DataSource = myDataSource
grdNotes.DataBind()
End If
If you are binding to a null/empty DataSource...then the GridView will not show up. You probably need to set the EmptyDataText property to something so that you can at least display a message when there is nothing to bind to.

Categories