I am using a grid view with auto generate column =true;
When I am updating the grid
protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
{
TextBox tPropertyId = (TextBox)row.FindControl("PropertyId");
}
is getting called.The proplem is thaT , tPropertyId is getting null value and when I am doing
tPropertyId.Text , I am getting null reference exception.
Please Help.
Please try this
protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
{
if (e.Row.RowType != DataControlRowType.Footer) return;
TextBox textBox = new TextBox();
e.Row.Cells[0].Controls.Add(textBox);
}
Related
I want to change my button text on page load after retrieving the list view values.
For example,
<asp:Label ID="favouriteLabel" runat="server" Text='<%# Eval("favourite") %>' />
If this label value is 1, the button will change to Favourited.
I have retrieved the list view values by binding the listview
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Label activity = (Label)e.Item.FindControl("favouriteLabel");
activityID = activity.Text;
}
}
then, I get the activityID and do a simple if-else check on the page load
protected void Page_Load(object sender, EventArgs e)
{
if (activityID == "1")
{
Button4.Text = "Favourited";
}
else
{
Button4.Text = "Favourite";
}
}
However it does not work. Anybody?
Do that inside a PostBack check in the load event, for instance:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (activityID == "1")
{
Button4.Text = "Favourited";
}
else
{
Button4.Text = "Favourite";
}
}
}
Read more about postback here
Page_Load happens before your ItemDataBound event so the activityId you are looking at in the Page_Load will never be 1.
Just put the code you have in the Page_Load into the ItemDataBoundEvent
I have some data in ObjectDataSource, before binding the data to the GridView, I want to remove some rows from the DataSource.
This is what I am trying:
protected void gvExitInterview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
User employee = (User) e.Row.DataItem;
if(//some condition here)
{
//do nothing
}
else
{
//delete the row
this.gv.DeleteRow(e.Row.RowIndex);
return;
}
}
}
These are my deletion methods:
protected void gvExitInterview_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
protected void gvExitInterview_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
gv.DataBind();
}
This is my grid binding:
private void BuildGrid(DateTime from, DateTime to)
{
this.objDS.TypeName = "EmployeeManagement";
this.objDS.SelectMethod = "GetEmployees";
this.objDS.SelectCountMethod = "GetEmployeesCount";
this.objDS.SelectParameters.Clear();
this.objDS.SelectParameters.Add("from", from.ToString());
this.objDS.SelectParameters.Add("to", to.ToString());
this.objDS.SelectParameters.Add("csvEntities", csv);
this.objDS.SelectParameters.Add("sortExpression", ViewState["SortColumn"].ToString());
this.gv.DataSource = objDS;
this.gv.DataBind();
}
This is not working, it does not filter or delete any data from the grid. Any idea how to do explicit deletion?
I think you need to write another SelectMethod in the DataObjectClass which get a parameter which you use pass to filter condition. So that just returning with the rows required to display.
i have a gridview with tempaltefield buttons,
i want to create a session with value of a cell in selected button row ,
can anyone help me i tryed this but didnt work:
protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
{
Session["mysession"] = GridView1.SelectedRow.Cells[1].Text;
}
First of all, if it's just a imagebutton in a templatefield, actually you don't select de row. This line will problably throw an exception because SelectedRow is null.
But if you are using a command to select, that's correct. Maybe your event (ImageButton1_Click1) is not assigned to your image (OnClick).
You can try something like this:
protected void Page_Load(object sender, EventArgs e)
{
try
{
//Add and event RowDataBound
grvGrid.RowDataBound += new GridViewRowEventHandler(grvGrid_RowDataBound);
}
catch
{
//...throw
}
}
protected void grvGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.Header)
{
//...
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Add an ImageButton foreach row in GridView
ImageButton ibtImageAlt = new ImageButton();
ibtImageAlt.ImageUrl = "App_Images/y.gif";
//ImageButton's ID will be the index of the row
ibtImageAlt.ID = e.Row.RowIndex.ToString();
ibtImageAlt.ForeColor = System.Drawing.Color.White;
ibtImageAlt.Font.Overline = false;
ibtImageAlt.Click += ibtImageAlt_Click;
}
}
catch
{
//...throw
}
}
protected void ibtImageAlt_Click(object sender, EventArgs e)
{
try
{
//Catch the ImageButton ID and the row in GridView
//An example to catch the value of the row selected by the ImageButton
Int32 intIndexRow = Convert.ToInt32(((ImageButton)sender).ID);
String strTest = grvGrid.Rows[intIndexRow].Cells[0].Text;
}
catch
{
//...throw
}
}
I have a asp.net page with a datalist with a textbox and a button on it, on page load the textbox gets text in it, if I change the text and press the button the text doesn't get updated.
What am I doing wrong?
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable table = CategoryAccess.GetProducts();
ProductList.DataSource = table;
ProductList.DataBind();
}
}
protected void btn_Click(object sender, EventArgs e)
{
string Name = textbox.Text;
CategoryAccess.UpdateProducts(Name);
}
}
I had same problem. I found that I put textbox.text = "xxx" in Page_Load() but outside if(!ispostback).
Try to add EnableViewState property in your textBox control and set the value to true.
e.g.
<asp:TextBox ID="textBox1"
EnableViewState="true"
MaxLength="25"
runat="server"/>
or you can do it programatically:
protected void Page_Load(object sender, EventArgs e)
{
textBox1.EnableViewState = true;
}
You need to bing again the new data...
protected void btn_Click(object sender, EventArgs e)
{
string Name = textbox.Text;
// you update with the new parametre
CategoryAccess.UpdateProducts(Name);
// you get the new data
DataTable table = CategoryAccess.GetProducts();
// and show it
ProductList.DataSource = table;
ProductList.DataBind();
}
I have the following issue:
I create a TextBox dynamically in my web page, its value is "initialVal" in the beginning.
Now I need to make a postback (not callback) to the server, and during this operation, I need to compute/change the value of my textbox to other value.
Here's an example:
protected void Page_Load(object sender, EventArgs e)
{
TextBox txtBox = new TextBox();
txtBox.ID = "newButton";
form1.Controls.Add(txtBox);
txtBox.Text = "initialVal";
if (IsPostBack && Session["change"] == null)
{
txtBox.Text = "change";
Session["change"] = true;
}
}
The problem: even if I change the value via code, the textbox will keep the text "initialVal". I feel this is something related to the view state, but I don't understand.
Coudl anyone please help me here?
Thanks.
Everytime you load the page it is running this:
txtBox.Text = "initialVal";
You should wrap this in a check for postback:
if (!Page.IsPostback)
{
txtBox.Text = "initialVal";
}
That said, onLoad is the wrong event to do the creation, for it to be valid in the early enough in the page lifecycle, use OnInit.
See this article on MSDN.
Here is the final code from #user2890888:
public partial class WebForm1 : System.Web.UI.Page
{
TextBox txtBox = null;
protected void Page_Init(object sender, EventArgs e)
{
txtBox = new TextBox();
txtBox.ID = "newButton";
form1.Controls.Add(txtBox);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtBox.Text = "initialVal";
}
if (IsPostBack && Session["change"] == null)
{
txtBox.Text = "change";
Session["change"] = true;
}
}
}
Create your dynamic textbox creation in !IsPostBack
protected void Page_Load(object sender, EventArgs e)
{
if(!isPostBack){
TextBox txtBox = new TextBox();
txtBox.ID = "newButton";
form1.Controls.Add(txtBox);
txtBox.Text = "initialVal";
}
if (IsPostBack && Session["change"] == null)
{
txtBox.Text = "change";
Session["change"] = true;
}
}
Thanks and let me know if your issue still pending
Find the TextBox and use it
TextBox txtBox = (TextBox)FindControl("txtBox");
txtBox.Text = "change";
Even now you are having issue and your textBox is not getting the value you needed.
Store your new value for TEXTBOX in a hiddenfield.
I mean ,
if (IsPostBack && Session["change"] == null)
{
hiddenfield1.value = "change";
}
and later in your page script, you can assign back the value in this hiddenfield1 to textbox.
$(document).ready(
{
$('#txtBoxID').value=$('#hiddenfield1').value;
}
);