How to capture an event in a ListView? - asp.net

I have a ListView that has a Button in the item template. Is there a way for me to identify which Item had it's button clicked from my OnClick event?
I was able to do it with the code below but it seemed crufty. Is there a better way to do this?
((ListViewDataItem)((Button)sender).Parent.Parent)
UPDATE:
Was able to implement using the NamingContainer method that one user suggested and then mysteriously removed his answer. Seems like a safer way to do than my original method:
((ListViewDataItem)((Button)sender).NamingContainer)

Yes, give the button a command name, then attach to ListView.ItemCommand; clicking the button fires this event, and it has some more specifics about the list item, such as a reference to it via e.Item.
HTH.

protected void RemoveButton_Click(object sender, EventArgs e)
{
ListViewDataItem item = ((ListViewDataItem)((Button)sender).NamingContainer);
//ListViewDataItem item = (ListViewDataItem)((LinkButton)sender).Parent;
int i = item.DisplayIndex;
DataTable dt = (DataTable)Session["cart"];
dt.Rows[i].Delete();
Listcart.DataSource = dt;
Listcart.DataBind();
Label Lblcart = (Label)Page.Master.FindControl("Lbitem");
Lblcart.Text = Listcart.Controls.Count.ToString();
Session["quantity"] = Lblcart.Text;
Session["cart"] = dt;
GrandTotal();
Session["amount"] = LbGrandTotal.Text;
}

Related

How to gain row value of button clicked in a radgrid's DetailTable

If I were to want to gain the value of a row in the master table in a template column button I would do:
" OnCommand=event>
Event
GridDataItem item = (GridDataItem)RadGrid1.Items[Convert.ToInt32(e.CommandArgumrnt)];
I am not sure what index the grid returns when the detailtable button is clicked and every time I try to access a column in that row I get an error saying column does not exist. I tried doing so:
GridDataItem item = (GridDataItem)RadGrid1.MasterTable.DetailTables[0].Items[Convert.ToInt32(e.CommandArgumrnt)];
This did not work as well.
Excuse me if I made any mistakes, I am on my mobile phone.
I have found a much simpler solution to my problem that doesn't if require me to use oncommand or command argument! Instead I use the simple Onclick.
protected void Button1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridDataItem item = (GridDataItem)btn.NamingContainer;
TableCell cell = (TableCell)item["UniqueName"];
string value = cell.Text;
}

Asp.net Dynamic controls dropdownlist and textbox prob

I
have created 2 drop down list and 2 text boxes dynamically in asp.net .i disable text box at run time .i want that when i select item from drop down text box should be enable how to perform this task please help me :(
On SelectedIndexChanged on the dropDownList call a function that sets the textbox enabled = true. To access controls that have been dynamically added you can use FindControl as per C#, FindControl
I think something like this should help you:
In your page's OnInit event:
DropDownList ddl = new DropDownList();
ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
placeholder.Controls.Add(ddl); //assuming this is what you use to dynamically show the dropdown list
TextBox yourTextbox = new TextBox(); //declare the variable outside to be able to be accessed by other methods, but it must be instantiated here. declaration here is for illustration purposes only
yourTextBox.Enabled = false;
placeholder.Controls.Add(yourTextBox);
Inside the instantiated event handler:
void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
yourTextbox.Enabled = true;
}

ASP.NET: How to access a dynamically created control

I'm creating a bunch of Checkboxes dynamically:
CheckBox chkRead = new CheckBox();
chkRead.ID = "chk1";
chkRead.AutoPostBack = true;
chkRead.CheckedChanged += new EventHandler(CheckBox_CheckedChanged);
CheckBox chkPost = new CheckBox();
chkRead.ID = "chk2";
chkPost.AutoPostBack = true;
chkPost.CheckedChanged += new EventHandler(CheckBox_CheckedChanged);
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
}
What I want to do is the following:
When I check the chkPost CheckBox I want the chkRead CheckBox to be checked as well
In the CheckBox_CheckedChanged event I only have access to the CheckBox that was clicked
but I don't know how to check the other checkbox from that event.
This is from memory, but you could do something like this:
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
CheckBox chkPost = (CheckBox) chk.NamingContainer.FindControl("chk2");
CheckBox chkRead = (CheckBox) chk.NamingContainer.FindControl("chk1");
if(chk == chkPost && chk.Checked)
{
chkRead.Checked = true;
}
}
This is assuming you want to do all this in code-behind, after postback. If you want to do it in javascript, that's a different question.
This also assumes that chk1 and chk2 are in the same naming container. If they aren't, things will get complicated.
Since it is your code that creates the checkboxes, you can store their references in a list or dictionary and retrieve them by id when needed.
If you want to do it dynamically you can add an attribute to the checkboxess you are interested in-- you can then loop over the Page.Controls collection and test that the control you are looping over has that attribute and then you can check, or uncheck it.
some pseudo code:
foreach(var control in Page.Controls)
if(typeof(Control) is CheckBox and ((CheckBox)control).Attributes["myAttr"] != null)
//check or uncheck it
In reading your comment about nested controls-- this might be a bit of a hassle-- I tend to agree with Igor, that you should put the id's in a collection as they are being added dynamically.
Could you paste code where you are creating these checkboxes? Is it "OnInit" or somewhere else? Are you putting these checkboxes in container, do you store these controls as global variables or create them in method?

Adding radio button event handler at runtime

I am trying to add a radio button click event at runtime.
Radiobutton button = new RadioButton();
button.GroupName = "buttonGroup";
button.OnCheckedChanged = "buttonGroup_OnCheckedChanged"; //I can't do this?
I know I can do this from the markup, but when I try to do this from the code behine, I cant find OnCheckedChanged.
Thanks for your help.
button.CheckedChanged += new EventHandler(buttonGroup_OnCheckedChanged);
button.CheckedChanged += buttonGroup_OnCheckedChanged;
I believe that is what you want.
Then you would obviously define buttonGroup_OnCheckedChanged and so on.
The handler should be created on page Init, the other answers are otherwise correct.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
button.CheckedChanged += new EventHandler(buttonGroup_OnCheckedChanged);
}

How to use the FindControl function to find a dynamically generated control?

I have a PlaceHolder control inside of a ListView that I am using to render controls from my code behind. The code below adds the controls:
TextBox tb = new TextBox();
tb.Text = quest.Value;
tb.ID = quest.ShortName.Replace(" ", "");
((PlaceHolder)e.Item.FindControl("ph_QuestionInput")).Controls.Add(tb);
I am using the following code to retrieve the values that have been entered into the TextBox:
foreach (ListViewDataItem di in lv_Questions.Items)
{
int QuestionId = Convert.ToInt32(((HiddenField)di.FindControl("hf_QuestionId")).Value);
Question quest = dc.Questions.Single(q => q.QuestionId == QuestionId);
TextBox tb = ((TextBox)di.FindControl(quest.ShortName.Replace(" ","")));
//tb is always null!
}
But it never finds the control. I've looked at the source code for the page and the control i want has the id:
ctl00_cphContentMiddle_lv_Questions_ctrl0_Numberofacres
For some reason when I look at the controls in the ListViewDataItem it has the ClientID:
ctl00_cphContentMiddle_lv_Questions_ctrl0_ctl00
Why would it be changing Numberofacres to ctl00? Is there any way to work around this?
UPDATE:
Just to clarify, I am databinding my ListView in the Page_Init event. I then create the controls in the ItemBound event for my ListView. But based on what #Womp and MSDN are saying the controls won't actually be created until after the Load event (which is after the Page_Init event) and therefore are not in ViewState? Does this sound correct?
If so am I just SOL when it comes to retrieving the values in my dynamic controls from my OnClick event?
UPDATE 2:
So i changed the code i had in my Page_Init event from:
protected void Page_Init(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//databind lv_Questions
}
}
to:
protected void Page_Init(object sender, EventArgs e)
{
//databind lv_Questions
}
And it fixed my problem. Still a little confused as to why I want to databind regardless of whether it's a postback or not but the issue is resolved.
It looks like you're adding your textbox to a Placeholder control... but then you're searching a ListViewDataItem container for it later.
Seems to me that you need to search for the Placeholder first, and then search it for the textbox.

Resources