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';");
}
}
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;
}
}
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());
}
}
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());
}
The problem really is that i do can trough the selects on the gridview get the data trough the id, but then i use the search option i implemented on the page and the gridview shows the ones it gets that match the result but if i press select it will redirect to the page with the wrong id, isntead of getting the id of the one i selected it gets the id of the field that was on the 1st position of the cell.
Here is the code:
protected void Page_Load(object sender, EventArgs e)
{
TeamGest.DBLayer.DBLTeams dbl = new TeamGest.DBLayer.DBLTeams();
GridView1.DataSource = dbl.List();
GridView1.DataBind();
TeamGest.DBLayer.DBLPlayers dbl1 = new TeamGest.DBLayer.DBLPlayers();
GridView2.DataSource = dbl1.List();
GridView2.DataBind();
}
protected void MyMenu_MenuItemClick(object sender, MenuEventArgs e)
{
{
MyMultiView.ActiveViewIndex = Int32.Parse(e.Item.Value);
int i;
for (i = 0; i <= MyMenu.Items.Count - 1; i++)
{
if (i == Convert.ToInt32(e.Item.Value))
{
MyMenu.Items[i].Text = MyMenu.Items[i].Text;
}
else
{
MyMenu.Items[i].Text = MyMenu.Items[i].Text;
}
}
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
Response.Redirect("DetalhesClube.aspx?Id="+row.Cells[0].Text);
}
protected void Button1_Click1(object sender, EventArgs e)
{
string searchStringTeam = TextBox1.Text;
GetTeamResults(searchStringTeam);
}
protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView2.SelectedRow;
Response.Redirect("DetalhesJogador.aspx?Id=" + row.Cells[0].Text);
}
protected void Button2_Click(object sender, EventArgs e)
{
string searchStringPlayer = TextBox2.Text;
GetPlayerResults(searchStringPlayer);
}
Don't use the cell values. That's what the DataKeys collection is for:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID, SomeOtherColumn" ...>
And in code-behind all you need is the row index:
var rowIndex = 0;
var ID = (int)GridView1.Rows[rowIndex]["ID"];