ASP.Net C# How to find a control in Listview - c#

I want to find a control in a selected row of a listview, can I do something like this:
lv_ListviewTest.Items(lv_ListviewTest.SelectedIndex).FindControl("ControlName")
What I had is:
if (e.CommandName == "Select")
{
var ctrl = (HtmlContainerControl)e.Item.FindControl("area");
ctrl.Attributes["style"] = "background-color:LightSkyBlue; color:Black; padding:0px;";
}
I can do it on Itemcommand, but I don't know how to trigger itemcommand when page re-load.
What I want to do is: when a button(not the button on the listview) clicked, the page reload and the focus will stop on the button of the listview.
Thank you very much.

you can find them below URL
Find and access controls in EditItemTemplate of ASP.Net ListView
How to use ListView control in ASP.NET

In Button click Event of Outer Button add below code
protected void MyButton_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in MyListView.Items)
{
var ctrl = (HtmlContainerControl)item.FindControl("area");
ctrl.Attributes["style"] = "background-color:LightSkyBlue; color:Black; padding:0px;";
}
}

Oh, I found that I can use SelectedIndex and Session variable to store it:
When item selected:
protected void lv_ListviewTest_SelectedIndexChanged(object sender, EventArgs e)
{
Session["SelectedIndex"] = lv_ListviewTest.SelectedIndex;
}
When the other button clicked:
protected void cmd_OtherButton_Click(object sender, EventArgs e)
{
...............
Button focusbutton = (Button)lv_ListviewTest.Items[Convert.ToInt16(Session["SelectedIndex"])].FindControl("MyControlLabel");
focusbutton.Focus();
}
So I can let my listview show what I selected before like ensurevisible do.

Related

get textbox that is in the selected tab

I have a tabcontrol with numerous tabs that all contain a textbox. How can I select the textbox that is in the currently selected tab?
I have this which captures the tabchanged event and tells me which tab is selected, but I cannot figure out how to find the textbox that is in the tab and do
textbox.Select(0, 0);
to select certain text in this textbox...
private void onTabChange(Object sender, TabControlEventArgs e)
{
}
This really sounds like a design mistake. High odds that this TextBox should not be on a tab page at all. If you want to have one text box to be present on every tab page then that's possible, Winforms makes it easy to move controls:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) {
textBox1.Parent = tabControl1.SelectedTab;
}
If you really meant for any text box to be picked, like the first one in the tab order then:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) {
var box = tabControl1.SelectedTab.Controls.OfType<TextBox>().Reverse().FirstOrDefault();
if (box != null) {
// etc...
}
}
Use This:
Tab TabView = (Tab)sender;
TextView txt_Tab = (TextView)TabView.FindControl("TextBoxName");
Try this:
TextBox myTB = tabControl2.SelectedTab.Controls[0] as TextBox;
myTB.Select(0, 0);
I think the following links can provide you some hints about your problem
How to access controls that are inside a TabControl tab?
and
How to get control(s) from TabPage in C#?

C# web form listbox OnSelectedIndexChanged

I have a listbox on my website, which contains some elements.
I made the event, OnSelectedIndexChanged, so when the user presses an element, this value will be put into a textbox
protected void Page_Load(object sender, EventArgs e)
{
listbox = new Listbox();
// Add to page etc.
listbox.SelectedIndexChanged += new EventHandler(listbox_SelectedIndexChanged);
}
void listbox_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
textbox_name.Text = listbox.SelectedItem.ToString();
}
catch
{
textbox_info.Text = "Choose employee";
}
}
It works in c# windows forms, but not in web forms for some reason.
Is it possible to get it to work?
Thank you
Set autopostback property of the listbox true

First mouse click event on an item in a listbox doesn't trigger the function

I am trying to do something like: when a user select an item on a listbox, then the function listboxClicked will be triggered. However, the first click often does not able to trigger the function. It only triggers the function when I click the same item or another item for the second time.
May I know what's wrong with my code? Thank you.
My Code:
private void listbox_SelectedIndexChanged(object sender, EventArgs e)
{
listbox.MouseClick += listboxClicked;
}
private void listboxClicked(object sender, EventArgs e)
{
if (listbox.SelectedIndex != -1)
{
//do something
}
}
Try this one:
Listbox1_SelectedValueChanged(object sender, EventArgs e)
{
Listbox listbox = (Listbox)sender;
MessageBox.Show(listbox.SelectedItem.ToString());
}

Page lifecycle question

I am using a DropDown menu and an UpdatePanel to filter out a DataGrid. The DataGrid has buttons which redirect to a different page. When I hit the back button or a link on top of the other page, it redirects me to the page with the DropDown as it should...but it gets rid of the DataGrid data and I have to make a selection from the DropDown again. Is there a way to make sure that the DropDown selection is remembered when both the link is pressed and the back button selected? Thanks for your help!
The easiest thing to do in this case is to save the dropdown selection in Session collection
and on page load, check to see if there is a saved selection and use it to reapply the selection.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Session["SavedSelection"] = DropDownList1.SelectedIndex;
}
protected void Page_Load(object sender, EventArgs e)
{
if(Session["SavedSelection"] != null)
{
int selectedIndex = (int) Session["SavedSelection"];
DropDownList1.SelectedIndex = selectedIndex;
}
}

get submit button id

Inside asp.net form I have few dynamically generated buttons, all of this buttons submit a form, is there a way to get which button was submit the form in page load event?
The sender argument to the handler contains a reference to the control which raised the event.
private void MyClickEventHandler(object sender, EventArgs e)
{
Button theButton = (Button)sender;
...
}
Edit: Wait, in the Load event? That's a little tricker. One thing I can think of is this: The Request's Form collection will contain a key/value for the submitting button, but not for the others. So you can do something like:
protected void Page_Load(object sender, EventArgs e)
{
Button theButton = null;
if (Request.Form.AllKeys.Contains("button1"))
theButton = button1;
else if (Request.Form.AllKeys.Contains("button2"))
theButton = button2;
...
}
Not very elegant, but you get the idea..
protected void Page_Load(object sender, EventArgs e) {
string id = "";
foreach (string key in Request.Params.AllKeys) {
if (!String.IsNullOrEmpty(Request.Params[key]) && Request.Params[key].Equals("Click"))
id = key;
}
if (!String.IsNullOrEmpty(id)) {
Control myControl = FindControl(id);
// Some code with myControl
}
}
This won't work if your code is inside a user control:
Request.Form.AllKeys.Contains("btnSave") ...
Instead you can try this:
if (Request.Form.AllKeys.Where(p => p.Contains("btnSave")).Count() > 0)
{
// btnSave was clicked, your logic here
}
You could try:
if (this.Page.Request.Form[this.btnSave.ClientID.Replace("_", "$")] != null) {
}
please try this code in page load event
string eventtriggeredCategory = Request.Form["ctl00$ContentPlaceHolder1$ddlCategory"];
if eventtriggeredCategory is returning any value its fired the event of ddlCategory
this is works fine for me
Thanks
Jidhu
Request.Form["__EVENTTARGET"] will give you the button that fired the postback
Use CommandArgument property to determine which button submits the form.
Edit : I just realized, you said you need this at PageLoad, this works only for Click server side event, not for PageLoad.

Categories

Resources