I'm having some trouble being able to access a control inside my repeater which is found in the ContentPlaceHolder. My previous method without the master page was working fine but now that I included a master page it threw a lot of things off mainly because of the naming. Here is the code that I previously had that worked without the master page:
LinkButton button = sender as LinkButton;
// Get index of LinkButton that was clicked
int repeaterItemIndex = ((RepeaterItem)button.NamingContainer).ItemIndex;
foreach (RepeaterItem myRepeater in rptrWebsites.Items)
{
TextBox myText = myRepeater.FindControl("Web") as TextBox;
var id = myText.ClientID;
// Get just the number part of the ID
id = id.Replace("rptrWebsites_Web_","");
if (repeaterItemIndex.ToString() == id)
{
webName = myText.Text;
}
}
The name of my ContentPlaceHolder is ContentPlaceHolderBody. I can probably find a way to do this using jQuery but I would prefer to keep this in the Codebehind. Any help is appreciated!
Not sure, but maybe this is what your looking for :
LinkButton button = sender as LinkButton;
// Get index of LinkButton that was clicked
int repeaterItemIndex = ((RepeaterItem)button.NamingContainer).ItemIndex;
foreach (RepeaterItem myRepeater in rptrWebsites.Items)
{
if (repeaterItemIndex == myRepeater.ItemIndex)
{
TextBox myText = myRepeater.FindControl("Web") as TextBox;
webName = myText.Text;
}
}
Anyway, I suggest you post more code and explain what you want to achieve. As your code seems fit for the ItemCommand pattern.
Edit : if the Button and the TextBox share the same naming container, this should also work :
LinkButton button = sender as LinkButton;
TextBox myText = button.NamingContainer.FindControl("Web") as TextBox;
webName = myText.Text;
This really seems like ItemCommand
Related
I'm working on a project in ASP.net (C#) and I have some trouble in concern with the asp:Panel.
Basically, I have an asp:UpdatePanel in which I have a asp:Panel;
I Also have asp:TextBox and a button.
What I want to do is that : when the user enters text in the asp:TextBox, and then hit the button, the text in the TextBox should Appear in the asp:Panel as a Linkbutton. The Method looks like this :
protected void AddExchange_Click(object sender, EventArgs e)
{
LinkButton link2 = new LinkButton();
link2.Text = AddAdditionalTxt.Text;
// link2.Command += new CommandEventHandler(LinkButton_Command);
ExchangePanel.Controls.Add(link2);
}
It works, and it adds the text as a linkButton to the panel, BUT, when i want to add more, the linkButton which has been added before get overwritten. I want to hold some LIST<string> Variable that'll hold all the strings, But whenever the page PostBack it gets deleted.
I would really appreciate if someone could tell me how to solve it - How do i keep a variable (List<>) in a page with UpdatePanel so every PageLoad I can add all the strings in the list inside the asp:Panel.
Thanks in advance.
this code works for me, the ViewState will keep the List<string> the time the user stays in the page
protected void AddExchange_Click(object sender, EventArgs e)
{
List<string> stringList;
if (ViewState["stringList"] == null)
stringList = new List<string>();
else
stringList = ViewState["stringList"] as List<string>;
stringList.Add(AddAdditionalTxt.Text);
foreach (string myStr in stringList)
{
LinkButton link2 = new LinkButton();
link2.Text = myStr;
// link2.Command += new CommandEventHandler(LinkButton_Command);
ExchangePanel.Controls.Add(link2);
}
ViewState["stringList"] = stringList;
}
Page 1 - Ticket.aspx, DropDownList1, ModalPopUpextender with id mpe
Page 2 - Customer.aspx, btnSave
The index change event of dropdown will pop up mpe which has an iframe. This iframe loads Customer.aspx.
I am trying to access page1 controls in the button click event, but unable to.
Customer.aspx.cs:
protected void btnSave_Click()
{
Ticket page = new Ticket();
ModalPopUpExtender mpe = (ModalPopUpExtender)page.FindControl("mpe");
DropDownList ddl = (DropDownList)page.FindControl("DropDownList1");
//error here - Object reference not set to an instance
mpe.hide();
ddl.selectedindex=0;
}
Why is this not working. Using a Session variable should work right?
You may use Server.Transefer instead of Response.Redirect and then you can find the control in the current page.
Like:
TextBox tb = (TextBox)PreviousPage.FindControl("textbox1");
EDIT:
if (Page.PreviousPage != null)
{
DropDownList ddl1 =
(DropDownList)Page.PreviousPage.FindControl("DropDownList1");
if (ddl1 != null)
{
Label1.Text = ddl1.SelectedItem.Text; //your logic
}
}
What you are trying may not be doable from the server side, but it can be easily be done with a little javascript. Here is a link where you can get a working piece of code.
Hope this helps.
I've got one long GridView control on ma website. It allows row selection. The problem is, when I scroll down this GridView and select some of the bottom rows the selection occurs, but whole GridView is scrolling back to top.
I refer this link for resolving my problem but not able to find any property into GridView control. I also search on msdn Link .
Please guide me where is this property and how can resolve my issue.Base on below checkbox event I select the row. After check the checkbox enable to delete and edit button.
protected void ChkChanged_Click(object sender, EventArgs e)
{
int count = 0;
foreach (GridViewRow gr in grdProducts.Rows)
{
CheckBox chkGrd = ((CheckBox)gr.FindControl("CheckBox2"));
ImageButton editbutton = gr.FindControl("btnEdit") as ImageButton;
ImageButton deleteButton = gr.FindControl("btnDeleted") as ImageButton;
if (chkGrd.Checked)
{
count++;
editbutton.Visible = true;
deleteButton.Visible = true;
if (count > 1)
break;
}
else
{
editbutton.Visible = false;
deleteButton.Visible = false;
}
}
if (count > 1)
{
foreach (GridViewRow gr in grdProducts.Rows)
{
CheckBox chkGrd = ((CheckBox)gr.FindControl("CheckBox2"));
ImageButton editbutton = gr.FindControl("btnEdit") as ImageButton;
ImageButton deleteButton = gr.FindControl("btnDeleted") as ImageButton;
if (chkGrd.Checked)
{
editbutton.Visible = false;
deleteButton.Visible = false;
}
}
DeleteAll.Enabled = true;
}
}
Can you give your 'row selection' code,is it posting back the entire page,If so, try to put the GridView in an UpdatePanel, so that the event is sent to the server without actually reloading the whole page.
Where are the scrollbars - to the page or to the grid-view? If possible, get rid of scrollbars to the grid-view and get scroll bars at the page level. In such case, you can use MaintainScrollPositionOnPostback which is a page property and it maintains page' scroll position on the post-back.
If your layout does not allow you to have scroll-bars at the page then I will suggest that you use a container div to the grid-view and have scroll-bars at the div level (instead of table i.e. grid-view) level. Now, you can have some JS code that can record the scroll position into the hidden field before page gets submitted and then same can use used after post-back to restore the position back. See this link where it uses such a trick : http://michaelsync.net/2006/06/30/maintain-scroll-position-of-div-using-javascript-aspnet-20
I found the answer .
In GridViewRow have Focus() method.
gr.Focus();
It's 95% set the position in your gridview selected Row.
I am trying to obtain the ctl property that is specific to each row in a gridview. In my scenario, there is a list of titles, each title is displayed as a hyperlink using the LinkButton. When this is clicked, I would like to pass the ctl property (or what ever value is specific to that title, to the database and pull the information that is relative to the value).
I guess my first step is to obtain the ctl value. Could someone please help me get on my way. Thanks in advance.
You will need to keep track of the control ID that is written to the page in the row created event. Make a integer to increment for each row written and save it as a command argument on a link in the grid.
linkbutton CommandArgument='<%# Eval("some_id") %>'
protected void linkButton_Click(object sender, EventArgs e)
{
LinkButton linkButton = (LinkButton) sender;
if (linkButton != null)
{
if (linkButton.CommandArgument != null)
{
...some code...
}
}
}
I have a GridView with a item template defined like:
public class ToolLogTemplate : ITemplate
{
public String DataField { get; set; }
public ToolLogTemplate(String column)
{
DataField = column;
}
public void InstantiateIn(Control container)
{
var textBox = new TextBox();
textBox.ClientIDMode = ClientIDMode.Predictable;
textBox.CssClass = "ToolLog";
textBox.AutoPostBack = true;
textBox.DataBinding += textBox_DataBinding;
container.Controls.Add(textBox);
}
void textBox_DataBinding(object sender, EventArgs e)
{
var textBox = (TextBox)sender;
var context = DataBinder.GetDataItem(textBox.NamingContainer);
textBox.Text = DataBinder.Eval(context, DataField).ToString();
}
}
The GridView is inside of a UpdatePanel defined like:
UpdatePanel updatePanel = new UpdatePanel();
updatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional;
The TextChanged event of the TextBoxes in the GridView trigger a full refresh of the page. My understanding was that by wrapping the TextBoxes in a UpdatePanel it would trigger a partial refresh instead. Am I misunderstanding this?
Update in response to the newest comment on the question:
I have some javascript attached to the textboxes:
currentTextBox.Attributes.Add("onFocus", String.Format("document.getElementById('RowTextBox').value = {0}; document.getElementById('ColTextBox').value = {1}; this.style.backgroundColor='#ffeb9c';", i, j));
currentTextBox.Attributes.Add("onBlur", "this.style.backgroundColor='#ffffff'");
It just sets the colors of the textbox and saves where it is in the gridview. The updatepanel works as expected with the menu and button I have in it, it's just the textboxes that cause a full postback.
Upon seeing update code, I have revised my answer...
The UpdatePanel needs to be told which controls it should respond to. You can do this by adding Triggers. In your case, you have TextBox controls within a GridView. These Textboxes are set to autopostback. Since they are within the GridView, I believe the GridView is treating them like a RowCommand. Using your original code, I would recommend you add the following:
UpdatePanel updatePanel = new UpdatePanel();
updatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional;
gridView.OnRowCommand = "GridViewRowCommand";
AsyncPostbackTrigger newTrigger = new AsyncPostbackTrigger();
newTrigger.ControlID = gridView.ControlID;
updatePanel.Triggers.Add(newTrigger);
Within your codebehind, you would need to do something like this:
protected void GridViewRowCommand(object sender, RowCommandEventArgs e)
{
var myTextBox = e.Row.FindControl("myTextBoxID");
// Do some work
}
Well unfortunately I was never able to make the model described here work. Instead I put an invisible button inside of the updatepanel and had javascript click it on the textboxes onchange event. I don't know why this method works and the TextChanged one doesn't, but that's how it ended up going down.