I have a DataList and I am binding it in page load when it IS NOT a postback, but still I receive a null reference exception when I try to access the DataItem in the ItemCreated event, any suggestion?
protected void Page_Load(object sender, EventArgs e)
{
AppPath = MapPath(HttpContext.Current.Request.ApplicationPath);
MainDS.ReadXml(AppPath + FileName);
DataView MyDV = new DataView(MainDS.Tables[0]);
DataList1.DataSource = MyDV;
DataList1.DataBind();
}
protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
{
Response.Write(e.Item.DataItem.ToString());
}
You need to check the item is ordinary item, not header or footer:
protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Response.Write(e.Item.DataItem.ToString());
}
}
Related
I have a usercontrol DmsRegisterPod which is being output within a repeater which is itself, inside an UpdatePanel. I have an event on the DmsRegisterPod called OnUpdated which I'm subscribing to in the repeaters ItemDataBound event e.g:
protected void rptPendingDmsRequests_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DealershipIRLink irLink = (DealershipIRLink)e.Item.DataItem;
DmsRegisterPod dmsRegisterPod = (DmsRegisterPod)e.Item.FindControl("ucDmsRegisterPod");
dmsRegisterPod.ValidationGroup = string.Format("dms-pod-{0}", e.Item.ItemIndex);
dmsRegisterPod.DealershipIRLink = irLink;
dmsRegisterPod.OnUpdated += dmsRegisterPod_OnUpdated;
}
}
private void dmsRegisterPod_OnUpdated(object sender, EventArgs e)
{
this.DataBind();
}
The event is setup as such in the usercontrol:
public event EventHandler OnUpdated;
private void Updated(EventArgs e)
{
if (this.OnUpdated != null)
{
OnUpdated(this, e);
}
}
It's being raised in the Accept click handler:
protected void btnAccept_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
this.DealershipIRLink.dms_account_number = txtDmsNumber.Text;
this.DealershipIRLink.id_dealer_ir_link_status = DealerIRLinkStatus.DealerIRLinkStatusIdentifier.Approved;
this._irLinkService.UpdateDealershipIRLink(this.DealershipIRLink);
this.Updated(e);
}
}
However, the handler, OnUpdated is always null so the event never gets raised. It's as though the control is losing the event binding somehow. Can anybody see what I've done wrong here?
Register the event handler in ItemCreated instead of ItemDataBound which is triggered only when you databind the control and not on every postback(required):
protected void rptPendingDmsRequests_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DmsRegisterPod dmsRegisterPod = (DmsRegisterPod)e.Item.FindControl("ucDmsRegisterPod");
dmsRegisterPod.OnUpdated += dmsRegisterPod_OnUpdated;
}
}
All other logic that depends on the datasource belongs to ItemDataBound:
protected void rptPendingDmsRequests_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DealershipIRLink irLink = (DealershipIRLink)e.Item.DataItem;
DmsRegisterPod dmsRegisterPod = (DmsRegisterPod)e.Item.FindControl("ucDmsRegisterPod");
dmsRegisterPod.ValidationGroup = string.Format("dms-pod-{0}", e.Item.ItemIndex);
dmsRegisterPod.DealershipIRLink = irLink;
}
}
Here is my code.
problem: When click om a row och on select the page is refreshing and i dont get the text in the lable17.text.
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
Label17.Text = row.Cells[2].Text.ToString() ;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.cursor='Pointer';this.style.backgroundColor='Yellow'");
}
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
Label17.Text = "you selected" + row.Cells[2].Text;
}
Is your GridView in an UpdatePanel? If not the entire Page will postback when you click on the Button. Also, make sure that if you are setting the Text of Label17 in the Page_Load event that you only do it the first time i.e.
public void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
Label17.Text = "Default Text";
}
}
I have a DataGridView, I want to select the cell of the first column.
Heres is my datagridview.Click method:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
name = dataGridView1.CurrentRow.Cells[0].Value.ToString();
}
Currently my name variable is null.
What am I doing wrong?
CurrentRow may not be set yet, so use the RowIndex property for the event argument. Try it this way:
void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
if (e.RowIndex > -1 && dataGridView1.Rows[e.RowIndex].Cells[0].Value != null) {
name = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
}
}
And just in case, make sure the event is wired up:
public Form1() {
InitializeComponent();
dataGridView1.CellClick += dataGridView1_CellClick;
}
You could do this:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
var view = (sender as DataGridView); //<-- notes this
var currentCellString = view.CurrentCell.Value.ToString();
}
Sometimes you need to grab the sender object at use that - because it will always be updated.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
object name = dataGridView1.Rows[e.RowIndex].Cells[0].Value;
MessageBox.Show(name.ToString() == string.Empty ? "myvalue" : name.ToString());
}
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();
}
}
I have written this code, but the attribute is failed to be added to the markup. what is the problem? thanks
protected void Page_Load(object sender, EventArgs e)
{
PycDBDataContext db = new PycDBDataContext();
IEnumerable<seller_profile> profs = from rows in db.seller_profiles select rows;
ProfilesView.DataSource = profs;
ProfilesView.ItemCreated += new DataListItemEventHandler(ProfilesView_ItemCreated);
ProfilesView.DataBind();
}
void ProfilesView_ItemCreated(object sender, DataListItemEventArgs e)
{
e.Item.Attributes.Add("OnMouseOver", "this.style.backgroundColor = 'lightblue';");
}
What you really want is the ItemDataBound event and not the ItemCreated event.
Rewrite like this and you would be fine.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataList ProfilesView;
PycDBDataContext db = new PycDBDataContext();
IEnumerable<seller_profile> profs = from rows in db.seller_profiles select rows;
ProfilesView.DataSource = profs;
ProfilesView.ItemDataBound += new DataListItemEventHandler(ProfilesView_ItemDataBound);
ProfilesView.DataBind();
}
}
private void ProfilesView_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor = 'lightblue';");
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor = 'white';");
}
}