Structure of my asp: repeater
repeater
updatePanel
label1 (rating)
button (updates rating)
some_picture (thing being rated)
/update panel
/repeater
Imagine the output of the above repeater containing 100 rows. (1 label, and 1 button on each row).
Goal: when I click the button, I want the appropriate label to be updated. I dont know how to do this.
I can reference a label via:
Label myLabel2Update = (Label)Repeater1.Controls[0].Controls[0].FindControl("Label1");
But ofcourse, it will be the same label each time (not necessarily the label that needs to be updated). I need to update the label that is on the same row as the button.
Any guidance would be appreciated.
Handle the ItemCommand event of the repeater. In your event handler check the Item property of the event arguments and use findcontrol on that. e.g.
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
Label Label1 = (Label)e.Item.FindControl("Label1");
}
Label1 will be the label in the same item as the button that was clicked.
Or in response to Dr. Wily's Apprentice's comment you could do the following
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "bClick":
Label Label1 = (Label)e.Item.FindControl("Label1");
/*do whatever processing here*/
break;
}
}
And then for each button specify the command name "bClick"
You will need a helper method to iterate through the hierarchy or use the
Control's FindControl(string id) method for that.
Example:
var stateLabel = (Label)e.Row.FindControl("_courseStateLabel");
I assume there's an event handler for the button? If so, you should be able to do
protected virtual void OnClick(object sender, EventArgs e)
{
var label = ((WebControl)clickedButton).Parent.FindControl("Label1");
}
Related
I have dynamically created a button using a function I made which is meant to increase the value of another button created using a similar function. I'm able to retrieve the name of the dynamically created textbox, but since it is dynamically created, I can't reference it.
I've tried to pass the textbox through as a parameter, but it doesn't appear that I'm able to pass extra parameters:
Button ButtonDecreaseValue= addButton(ItemName, "-");
TextBox TextBoxItemAmount = addTextBox(ItemName, "0");
So, how would I change the value of textbox one when ButtonRemoveItem is clicked?
I've created an event handler for the button, like so:
ButtonIncreaseValue.Click += new EventHandler(ItemDecreased);
But, inside the event handler, I'm unsure how I would change the textbox name.
private void ItemDecreased(object sender, EventArgs e)
{
Button currentItem = (Button)sender;
int ItemNo = Convert.ToInt32(currentItem.Tag);
DataRow ItemInfo = getItemDetails(ItemNo);
ItemInfo[0].ToString();
}
When clicking the button, the corresponding textbox should decrease in value.
Assuming this is Windows Forms
You said you have the name of the TextBox? You should be able to find it with that.
private void ItemDecreased(object sender, EventArgs e)
{
string textBoxName = HoweverYouGetTheDynamicTextBoxName();
// Assumptions:
// 1. ItemDecreased is a method on a Form or a UserControl.
// 2. There is only one TextBox with the given name in the Form or UserControl.
// 3. The TextBox is added to the Form or UserControl's Controls property.
TextBox tb = Controls.Find(textBoxName, true).OfType<TextBox>().SingleOrDefault();
if (tb is null)
{
// Couldn't find the text box for some reason.
}
// tb references the dynamically created text box.
}
I have 2 gridviews which update some data, they are independent each one,
both are using "onrowupdating="actualizar"
i want to use the same event like below:
protected void actualizar(object sender, GridViewUpdateEventArgs
e)
{ }
How can i select the specific grid view inside the event?
The object sender will be the GridView who started the event so you can cast it to GridView and you will have it's properties like its Id.
Example:
(sender as GridView).ID
Thats the answer !!
Control cntrl = sender as GridView;
var gridId = cntrl.ID;
I have the following dispatch routine in VS2013 C#:
private void B_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
string src = btn.Name.ToString();
string foo = "G" + src.Substring(1);
G0.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
}
It currently changes the visibility of G0. I want to change the code so that if Button B123 is pressed, then G123.Visibility is changed.
Thanks,
Dan
Note: This is a generic eventhandler for the buttons. There are 100's of buttons so an individual handler for each button is not practical. It could also be the handler from a dropdown or text box. G123 is a random control on the XAML page. The point is, given a string that contains the Name, how do I find the associated control so that I can modify its properties?
I'm not sure that I correctly understand your question, so I may be guessing that if button buttons B123 and G123 are related to each other by the number 123. In general, I suppose you want to change the visibility of button GX if button BX is changed.
In order to find all controls in the Window, have a look at the solution provided by Bryce Kahle, see Find all controls in WPF Window by type. In your buttenclick handle, do something like
private void B_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
string src = btn.Name.ToString();
string identifier= src.Substring(1);
foreach (var btn in FindVisualChildren<Button>(this).Where(b => b.Name.EndsWith(identifier)))
{
if(btn.Name.StartsWith("G"))
btn.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
}
}
Hope that helps.
In the comments, user Clemens gave the answer. (Since he didn't give it as an answer, I can't vote it up.)
Using FindName, I was able to get to the properties of the desired control:
private void B_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
string src = btn.Name.ToString();
string foo = "G" + src.Substring(1);
Windows.UI.Xaml.Shapes.Rectangle rect = (Windows.UI.Xaml.Shapes.Rectangle)this.FindName(foo);
rect.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
}
This has the flexibility so that I can change the fill, contents, text, foreground, style, etc. for a specified control. More control than if I had simply used XAML binding.
Thanks Clemens,
Dan
I have a button, when clicked, a modalpopup with gridview inside it. as below;
protected void grdDetails_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = grdDetails.SelectedRow;
//Note: Value of that row's cell must display in A text box (txtBox) on main page
txtBox.Text = row.Cells[2].Text;
ChargeFilterModalDialogExtender.Hide();
}
After the extender is hide, the value of the cell not displaying in the text box. Am I doing something wrong?
Where you have the textbox?
Whether you are using UpdatePanel?, if so define the needed asynchronous postback triggers.
I have dropdownlist inside a repeater and whenever the selected text is changed i have to show it in a textbox how can i do this??
protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("DropDownList6");
TextBox txt = (TextBox)e.Item.FindControl("TextBox4");
txt.Text = ddl.SelectedItem.Text;
}
First, don't use ItemCreated therefore since it triggered too early in the life-cycle(for the ViewState). You would also have to check for the ItemType first.
Instead use the DropDownLists SelectedIndexChanged event directly:
protected void Ddl_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList) sender;
RepeaterItem item = (RepeaterItem) ddl .NamingContainer;
TextBox txt = (TextBox) item.FindControl("TextBox4");
txt.Text = ddl.SelectedItem.Text;
}
you could add appropriate OnSelectedChange (somwthing) event handler to the DropDownList and then when event fired you catch it and do whatever you want , you can do it in both client side or server side .
You will need to use the add a handler to associate each dropdown control with the appropriate event handler. I don't have VS in front of me but it should be something like:
txt.SelectedIndexChanged += new EventHandler(YourMethodName)