I am using a dropdownlist in footer row of gridview(ASP.Net) and I fill that on rowdatabound event,first time it works fine but when the form is postbacked,dropdown gets cleared.
It can be solved by refilling it on each postback,but I want that only single time code binding call fulfill my need,
means is there any way to stop from being null on postback.
looking for your kind solutions and suggestions
Thnx in advance......
Supriya
Code:
protected void gvProjects_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (gvProjects.Rows.Count > 0 && e.Row.RowIndex != -1)
{
string proj_Id = Convert.ToString(gvProjects.DataKeys[e.Row.RowIndex].Value);
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlProject = (DropDownList)e.Row.FindControl("ddlProject");
if (ddlProject != null && ddlProject.Items.Count == 0)
{
objMaster.FillProjects(ddlProject);
ddlProject.SelectedValue = proj_Id;
}
}
}
}
catch (Exception excep)
{
lbl_msg.Text = excep.Message;
}
}
It's called whenever the grid is binded,can it be avoided.
With this code you will avoid filling the dropdownlist located in the gridview footer in each rowdatabound:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.Footer)
{
//do the binding for the normal rows
}
}
As you can see, the rowdatabound will be only executed on the header and normal rows but not in the footer.
Hope this helps!
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDropdown();
}
}
Related
How to check, whether a cell in a <asp:CheckBoxField/>, which was bounded to bool column in database, is or is not checked (i.e. True or False)?
For instance some function like
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.Cells[myCheckBoxFieldIndex].IsChecked()) {do my stuff}
}
would do perfectly.
All I need is to access the value in the cell.
Thanks in advance!
First cast the control to a CheckBox
var cell = e.Row.Cells[myCheckBoxFieldIndex];
var checkbox = cell.Controls.OfType<CheckBox>().FirstOrDefault();
Then use the Checked property
if (checkbox != null && checkbox.Checked) { /* Do stuff */ }
e.Row.Cells[myCheckBoxFieldIndex] is type of DataGridViewCell.
You should cast this to DataGridViewCheckBoxCell
var checkbox = e.Row.Cells[myCheckBoxFieldIndex] as DataGridViewCheckBoxCell;
if (checkbox != null && checkbox.Checked)
{
// your code
}
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ( ((CheckBox )e.Row .Cells [0].Controls [0]).Checked ==true )
{
//do your stuff
}
}
}
I am using bound fields and inspecting cell text in rowdatabound event. When it's not a postback there's something in cell text. But when I cause a postback cell text is blank but it doesn't show blank on the display (e.Row.Cells[2].Text is what I'm inspecting below)
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[2].Text != tbUserName.Text)
{
LinkButton b = e.Row.Cells[1].Controls[0] as LinkButton;
b.Visible = false;
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
IEnumerable<Task> tasks = _dbc.Tasks.ToList();
GridView1.DataSource = tasks;
GridView1.DataBind();
}
You need to keep track whether page is being rendered for the first time or is being loaded in response to a postback. Currently gridview is binded again and again on every postback.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
IEnumerable<Task> tasks = _dbc.Tasks.ToList();
GridView1.DataSource = tasks;
GridView1.DataBind();
}
}
This was user error! I had some code in another area modifying the columns unexpectedly, my apologies.
i have a gridview in which i have created dynamic rows in row created event as shown in below code.
protected void grdPBook_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
strPreviousRowID = DataBinder.Eval(e.Row.Date).ToString()}
grdPBook.ShowFooter = false;
}
}
protected void grdPBook_RowCreated(object sender, GridViewRowEventArgs e)
{
bool IsSubTotalRowNeedToAdd = false;
bool IsGrandTotalRowNeedtoAdd = false;
if (ddlSummary.SelectedValue == "0")
{
if ((strPreviousRowID != string.Empty)(DataBinder.Eval(e.Row.DataItem,Date) != null))
{
if (strPreviousRowID != DataBinder.Eval(e.Row.DataItem, Date).ToString())
{
if (ddlSummary.SelectedValue == "0")
{
IsSubTotalRowNeedToAdd = true;
}
}
}
if (IsSubTotalRowNeedToAdd)
{
// ---code for adding dynamic subtotal row-----
}
}
}
when i print the grid the print dialog opens and after closing the dialog the dynamically generated columns of grid disappers and the grid gets messed up coz i m not able to retain the values for Date(here)on the basis of which the dynamic rowsare generated.
How can i achieve the task.Help me.
protected void grdPBook_RowCreated(object sender, GridViewRowEventArgs e)
{
// your code
if (ddlSummary.SelectedValue == "0")
{
//your code
grdPBook.DataSource = dt;
//here dt is your Data Table object containing all rows.
}
grdPBook.DataBind();
}
Use grdPBook.DataBind(); in every Row operations.
I have a gridview that has linkbuttons that call modalpopups and textboxes with values. I am trying to implement sorting for the gridview, but the if(!ispostback) statement I need for sorting prevents the modalpopup from appearing. It also does not sort the textboxes in the gridview. Is there a way to implement sorting without using ispostback in the page_load?
Here is the code for the modalpopup, gridview binding and sorting.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["sortOrder"] = "";
Bind_Gridview("", "");
loadModals();
}
}
protected void viewModal(object sender, EventArgs e)
{
...
mainPanel.Controls.Add(exstModal);
mainPanel.Controls.Add(exstModalBox);
exstModalBox.Show();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
Bind_Gridview(e.SortExpression, sortOrder);
}
public string sortOrder
{
get
{
if (ViewState["sortOrder"].ToString() == "desc")
{
ViewState["sortOrder"] = "asc";
}
else
{
ViewState["sortOrder"] = "desc";
}
return ViewState["sortOrder"].ToString();
}
set
{
ViewState["sortOrder"] = value;
}
}
protected void gv1_RowCommand(object sender, GridViewRowEventArgs e)
{
...
CheckBox cb = new CheckBox();
TextBox ca = new TextBox();
ca.Width = 20;
TextBox cga = new TextBox();
cga.Width = 20;
if (e.Row.RowType == DataControlRowType.DataRow) //Foreach row in gridview
{
while (dr1.Read())
{
ca.Text = dr1["cyla"].ToString();
cga.Text = dr1["cga"].ToString();
checkText = dr1["completed"].ToString();
if (checkText == "True")
{
cb.Checked = true;
}
else
{
cb.Checked = false;
}
}
...
dr1.Close();
conn1.Close();
e.Row.Cells[6].Controls.Add(ca);
e.Row.Cells[8].Controls.Add(cga);
e.Row.Cells[9].Controls.Add(cb);
...
}
A GridView has built-in sorting capabilities. Depending on the dataset you are using to populate the data, you likely don't need to manually handle anything manually, let alone with the ViewState.
Check out the second example on this MSDN page and note that it never does anything manually with the ViewState... the OnSorting and OnSorted events are there just to display extra information or to impose requirements:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting.aspx
If you post a bit more of your code (including your .aspx pages, the markup for the modal popups, and the code for the loadModals() function, we might be able to better help you.
I am using a gridview in aspx page. I am adding Textboxes dynamically to the griview header. How can I insert a value to the textbox in the grid header.
My code goes like
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
TextBox txtBox1 = default(TextBox);
for (int i = 0; i <= (e.Row.Cells.Count - 1); i++)
{
litHeader.Text = e.Row.Cells[i].Text + "<br/>";
e.Row.Cells[i].Controls.Add(litHeader);
e.Row.Cells[i].Controls.Add(txtBox1);
}
}
}
I am trying to bind a value to the textbox 'txtBox1' using the code
private void FillGridView()
{
TextBox txt;
txt = (TextBox)GridView1.HeaderRow.FindControl("txtBox1");
txt.Text = dtValues.Rows[0][5].ToString();
}
But I am not able to fill the textbox. Please help.
This may help someone on very old question. This how I update my header row text box immediately after I insert data to GV1. Hope it helps.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
}
if (e.Row.RowType == DataControlRowType.Header)
{
//((TextBox)GridView1.HeaderRow.FindControl("tbProjNumInsert")).Text
if (newMaxNum != null && newMaxNum.Length > 0)
{
((TextBox)e.Row.Cells[4].FindControl("tbProjNumInsert")).Text = newMaxNum;
System.Diagnostics.Debug.WriteLine("GridView1_RowDataBound()...GridView1.Width !!!! " + ((TextBox)e.Row.Cells[4].FindControl("tbProjNumInsert")).Text + "........newMaxNum:" + newMaxNum);
}
}
}