I have a grid and I need to get readonly textbox value in gridview c# from code behind
protected void grd1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (txt.Text != "")
{
txt.Attributes.Add("readonly", "readonly");
txt.Visible = false;
lbltaskname.Visible = true;
}
else
{
txt.Attributes.Remove("readonly");
}
}
}
You can do itself on aspx page.
Assuming that dataset is attached(bind) to the grid from code behind, now coming to gridview on aspx page , add textbox control to grid , in text property of texbox assign value from dataset by -
<%# Bind("your_column_name")%>
and add
readonly = true
to textbox.
Thanks
Related
How can I add readonly attribute at a control inside itemtemplate of Gridview by using code behind?
I have tried using rowdatabound but always get null
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){
if(e.Row.RowType == DataControlRowType.DataRow)
{
Textbox mytxt = e.Row.FindControl("txtDate") as Textbox;
}
}
I tried add readonly attribute in asp.net page but i cant get the textbox value(i use javascript calendar to input the textbox)
try
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){
if(e.Row.RowType == DataControlRowType.DataRow)
{
TextBox mytxt = e.Row.FindControl("txtDate") as TextBox;
mytxt.Attributes.Add("readonly", "readonly");
}
}
I have a gridview and I have to bind data in gridview as well as the footer of the gridview. I have tried many things but nothing is working. Can anyone tell me what I have to do can? I bind the gridview in the footer with the same query or I have to write diffrent query for gridview and footer. Below is my work. What I have tried I write diffrent query for both gridview and footer
System.Web.UI.WebControls.GridView grdview = ystem.Web.UI.WebControls.GridView)sender);
Label lblenqid = (Label)e.Row.FindControl("lblenqid1");
Label lblactualcost = (Label)grdview.FooterRow.FindControl("Label44") as Label;
Label lblpackagecost = (Label)grdview.FooterRow.FindControl("Label42") as Label;
Label lbldriver = (Label)grdview.FooterRow.FindControl("Label46") as Label;
objHotel.Tour_DetailVehiclePackageCost(lblenqid.Text,
blactualcost,lblpackagecost,lbldriver);
plz help me thanks in advance
protected void yourNiceGridViewControl_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
TextBox myTextBox = e.Row.FindControl("txtFooter") as TextBox;
if( myTextBox != null )
{
myTextBox.Tex= ds.Tables[0].Rows[0]["MyFirend"].ToString();
}
}
}
Try this.
I want to get the Text property from a TextBox that is inside a GridView. This TextBox has some data that come from my database. When I change this data, I wanna do a Update in my database.
But when I search for the Text of my TextBox, he get the old value that come from my database and not the value that I put now.
How can I do, to get my actual data that I write in my TextBox and not that come from my databse ?
protected void VerifyPriority()
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow RowView = (GridViewRow)GridView1.Rows[i];
TextBox txt = (TextBox)GridView1.Rows[i].FindControl("txtPriority");
if (txt != null)
{
if (txt.Text != this.changes[i])
{
this.UpdatePriority(this.codigo[i], txt.Text);
}
}
}
}
More than likely you're you're rebinding the GridView after every postback instead of binding it once and letting ViewState persist the data. If you bind the GridView every time the page is posted back, any changes you make will be wiped out and replaced with the information from the database.
Do your binding on the first page load only (in this case):
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.DataSource = GetSomeData();
GridView1.DataBind();
}
}
After putting the above code in place, you should be able to get the correct data from the TextBox:
foreach (GridViewRow row in GridView1.Rows)
{
TextBox txt = row.FindControl("TextBox1") as TextBox;
if (txt != null)
{
string value = txt.Text;
}
}
The process I am doing is if lbl.Text is "Validated" then disable the checkbox accordingly in grid. The code works fine if paging is not there.
Now the problem is I am using paging and when I click to the next page of grid
the validated things appear with the checkbox enabled.
I checked through breakpoints. Its loading the previous gridpage values during page load event. And after pageloadevent its going design and loads the new values in grid.
//-------loading previous page values of grid here---------
protected void Page_Load(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
Label lbl = (Label)row.FindControl("Labely8");
Label Label23 = (Label)row.FindControl("Label23");
CheckBox checkbox = (CheckBox)row.FindControl("chkRows");
if (lbl.Text == "Validated")
{
checkbox.Enabled = false;
}
else
{
checkbox.Enabled = true;
}
}
}
I think you need to do enable or disable each of the checkboxes individually in the GridView.RowDataBound event rather than all at once in the Page_Load event:
void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox checkbox = (CheckBox)e.Row.FindControl("chkRows");
checkbox.Enabled = e.Row.Cells["nameOfCellWithLabel"].Text == "Validated";
}
}
I am making conditional formatting changes to the data in my gridview using a RowDataBound event:
void gvReg_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DateTime lastUpdate DateTime.Parse(DataBinder.Eval (e.Row.DataItem, "LAST_UPDATE");
if (lastUpdate < DateTime.Today.AddMonths(-1))
{
Hyperlink hypLastUpdate = (Hyperlink)e.Row.FindControl("hypLastUpdate";
hypLastUpdate.CssClass = "Error";
hypLastUpdate.NavigateUrl = "http://www.someExampleErrorPage.com";
}
}
}
This works, and sets the proper CssClass to the hyperlink (which makes it a jarring shade of bold red), but once the gridview is sorted (via the user clicking a column heading) the css class is reset on hypLastUpdate and it loses both it's style and associated NavigateUrl property.
The control hypLastUpdate is contained in a template field in a gridview, and it's text value is databound to a field called "LAST_UPDATE".
Is this a planned behavior (is sorting supposed to break the conditional formatting done in RowDataBound events?) or is there something I can check to make sure I am not doing something incorrectly?
I am not using the DataBind method anywhere in the code behind, and viewstate is turned on for the gridview in question.
--EDIT--
It ended up being a mistake in event handling.
I was doing:
gvReg.Sorted += {SomeEventHandler}
Inside of the page load event, but only when it wasn't a postback. This function called gvReg.DataBind after the grid view was sorted. I removed the handler wire up and instead added the event handler function to the OnSorted event. I guess assigned delegates to a gridview are not saved in ViewState between callbacks?
Hi here is a quick example of what I meant on my comment. This is the only way i could think of it:
protected void gvReg_Sorting(object sender, GridViewSortEventArgs e)
{
GridView gridView = (GridView)sender;
if (e.SortExpression.Length > 0)
{
foreach (DataControlField field in gridView.Columns)
{
if (field.SortExpression == e.SortExpression)
{
cellIndex = gridView.Columns.IndexOf(field);
break;
}
}
if (pSortExpression != e.SortExpression)
{
pSortDirection = SortDirection.Ascending;
}
else
{
pSortDirection = (pSortDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending);
}
pSortExpression = e.SortExpression;
}
//Retrieve the table from the database
pSortOrder = pSortDirection == SortDirection.Ascending ? "ASC" : "DESC";
List<Partners> partnerList = GetPartnerList();
gvReg.DataSource = partnerList;
gvReg.DataBind();
}