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.
Related
Inside a ListView Control's <ItemTemplate> I'm using a LinkButton.
When the List populates it has a set of LinkButtons. The link button text's are generated from a column in the records retrieved using a data source.
When I click on a LinkButton, I need it's text to be captured into either a hidden field or view state during the post back, so that it will be displayed in a Label or TextBox when page post back happens.
But it does not happen on first page post back. Instead, I have to click on the LinkButton twice for two post backs for the value to be displayed in Label/TextBox.
How can I get it done in the first post back ?
I have tried the same without the ListView, using just a LinkButton as below, and get the same outcome.
protected void LinkButton_Click(object sender, EventArgs e)
{
LinkButton selectedButton = (LinkButton)sender;
HiddenField1.Value = selectedButton.Text;
ViewState["LinkButtonText"] = selectedButton.Text;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(HiddenField1.Value))
{
Label1.Text = HiddenField1.Value;
}
TextBox1.Text = HiddenField1.Value;
if (ViewState["LinkButtonText"] != null)
{
if (!string.IsNullOrEmpty(ViewState["LinkButtonText"].ToString()))
{
ViewStateTextBox.Text = ViewState["LinkButtonText"].ToString();
}
}
}
Well, It happens since the sequence of the server side method execution. The page load before hand, then the control click methods, in that order. Instead of updating hidden field like that now using a client side JavaScript function OnClientClick of the LinkButton control, which updates the hidden field.
In short, you use it everytime you need to execute something ONLY on first load.
The classic usage of Page.IsPostBack is data binding / control initialization.
if(!Page.IsPostBack)
{
//Control Initialization
//Databinding
}
Things that are persisted on ViewState and ControlState don't need to be recreated on every postback so you check for this condition in order to avoid executing unnecessary code.
Another classic usage is getting and processing Querystring parameters. You don't need to do that on postback.
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.
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...
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();
}
Okay, I have a FormView with a couple of child controls in an InsertItemTemplate. One of them is a DropDownList, called DdlAssigned. I reference it in the Page's OnLoad method like so:
protected void Page_Load(object sender, EventArgs e)
{
((DropDownList)FrmAdd.FindControl("DdlAssigned")).SelectedValue =
((Guid)Membership.GetUser().ProviderUserKey).ToString();
}
Basically I'm just setting the default value of the DropDownList to the user currently logged in.
Anyway, when the page finishes loading the SelectedValue change isn't reflected on the page. I stepped through OnLoad and I can see the change reflected in my Watch list, but when all is said and done nothing's different on the page.
I figured it out. I'm still missing exactly why it doesn't work just on FormLoad, but performing the change in the FormView's DataBound event does the trick.
protected void FrmAdd_DataBound(object sender, EventArgs e)
{
// This is the same code as before, but done in the FormView's DataBound event.
((DropDownList)FrmAdd.Row.FindControl("DdlAssigned")).SelectedValue =
((Guid)Membership.GetUser().ProviderUserKey).ToString();
}
So, I guess the general rule of thumb is that if you are having problems making changes to controls when working with databinding, try to make them immediately after it has been bound.
I had a problem with dropdownlists and making the first value say something like, "Please select a value..." but without making it an actual selectable item, nor show up on the dropdownlist. I was binding the ddl in the page_load and I have to make sure that I set the text of the dropdownlist, AFTER it's been bound with data. You've accomplished the same thing by adding it to your databound section.