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.
Related
I working with ASP.NET WebForms with C#. I have a button when clicking on it creates a dropdownlist. This list is created in the "createlist" method with parameters such as id and items. I also add a SelectedIndexChanged event handler.
The list is created successfully, but when I switch between the 3 options the handler is not firing, because the console never prints the message "CHANGE".
Here's my code:
namespace web
{
public partial class Default : System.Web.UI.Page
{
List<string> lstDivs = new List<string>();
protected void btn_Click(object sender, EventArgs e)
{
Control results = FindControl("results");
lstDivs.Add("One");
lstDivs.Add("Two");
lstDivs.Add("Three");
DropDownList lstTipo = createList("lstTipos", lstDivs);
results.Controls.Add(lstTipo);
}
public DropDownList createList(string id, List<string> lstStr)
{
DropDownList lst = new DropDownList();
lst.Attributes.Add("runat", "server");
lst.ID = id + "" + Variables.numDiv;
lst.SelectedIndexChanged += new EventHandler(change);
lst.AutoPostBack = true;
lst.Items.Add(new ListItem("Select...", "Select..."));
for (int i = 0; i < lstStr.Count; i++)
{
lst.Items.Add(new ListItem(lstStr[i], lstStr[i]));
lst.Items[i].Attributes.Add("id", lst.ID + "" + i.ToString());
}
return lst;
}
protected void change(object sender, EventArgs e)
{
Debug.Write("CHANGE\r\n");
}
}
}
Once a dynamic control is created and displayed to the user, it has to be recreated on a postback for its events to fire. Execution of events is triggered by the control itself, hence, if there is no control - there is nobody to detect the change and call your change method.
You have to persist the fact that the dropdown has been created (hidden field, ViewState, Session, database, ...) and create it again in Page_Load.
I'm not sure your control exists on the postback since it is dynamically created. I found this post which might be helpful:
Dynamically Added DropDownlists Are Not Firing SelectedIndexChanged Event
Also, I don't think you need to add the runat as an attribute. This should be done automatically.
- Primary Info:
In my recent project, I need to have a page with a DropDownList with some items like 'firstName' , 'lastName' , 'Age' and etc. I want to add optional controls to the page when every item selected by user. For example when user select the 'Age' another dropdownlist created dynamically with these values : 'Less than 10'
'Between 10 and 30'
'more than 30'
Here is a button that add this user selection to listBox and let user to choice another options. (I made a query at last according to user choices and send it to db)
- What I do:
I create a dropDownList and set it's AutoPostBack property to true and adds some items in it and user must select one of those item. then I add user SelectedValue of dropDownList in a Cache variable before page post back happens:
protected void DropDownListColumnNameSelectedIndexChanged(object sender, EventArgs e)
{
Cache["SelectedKey"] = dropDownListColumnName.SelectedValue;
}
When user select an item from dropDownList *DropDownList_SelectedIndexChanged* fire, and I must create controls dynamically in a place holder:
var textBoxName = new TextBox
{
ID = "textBoxName",
CssClass = "str-search-textbox-highlight",
ViewStateMode = ViewStateMode.Disabled
};
placeHolderFirstItem.Controls.Add(textBoxName);
- What is the problem?
When I try add new control in current Button_Click event, control added successfully to page but I can't find it by placeHolderFirstItem.Controls.Find("textBoxName") actually placeHolderFirstItem.Controls.Count is always zero. So I can't get textBoxName.Text values.
I try to google that for any solution and I found some solution that I must add controls in Page.OnInit so I add controls in overridden OnInit(e):
protected override void OnInit(EventArgs e)
{
if (!Page.IsPostBack) return;
var textBoxName = new TextBox
{
ID = "textBoxName",
CssClass = "str-search-textbox-highlight",
ViewStateMode = ViewStateMode.Disabled
};
placeHolderFirstItem.Controls.Add(textBoxName);
}
after doing this I can find "textBoxName" in placeHolderFirstItem, but it fire before DropDownList_SelectedIndexChanged !
so how can I add new controls to place holder exactly when user change the dropDownList value and how can I read new controls value?
Thanks in advance,
Mohsen.
- Updated:
Here is the better solution
(http://forums.asp.net/p/1959726/5596531.aspx?p=True&t=635244790943067485&pagenum=1)
When you are dynamically adding controls, you have to reload the controls into the control tree everytime thereafter for it to appear. With the help of viewstate, you could change your code sample to have:
ViewState("ShowTextbox") = true
And then in your init routine:
protected override void OnInit(EventArgs e)
{
if (!Page.IsPostBack) return;
if (ViewState("ShowTextBox") == true) {
var textBoxName = new TextBox
{
ID = "textBoxName",
CssClass = "str-search-textbox-highlight",
ViewStateMode = ViewStateMode.Disabled
};
placeHolderFirstItem.Controls.Add(textBoxName);
}
}
Please note it's much easier to have a control on the control tree, and then show/hide by setting Visible to true/false, because of these ASP.NET control tree issues.
I have a custom control that has a search function. When the user clicks search, the code generates a list of buttons based on the results of the search.
Each button's CommandArgument property contains the Id of the search result. When the user clicks the button, I want to handle the event, get the CommandArgument and raise an event back to the main page with the value.
The problem is that the buttons aren't raising the event. I would assume this is because it gets lost on a postback or whatever. Should I consider an alternative way of listing the values to make this work?
Whats the simplest way of getting around this? Here's what I have:
private void Search()
{
foreach (var c in companies)
{
TableRow companyRow = new TableRow();
TableCell nameRowCell = new TableCell();
TableCell regRowCell = new TableCell();
Button selectButton = new Button();
selectButton.CommandArgument = c.Id.ToString();
selectButton.Text = c.Name;
selectButton.Click += new EventHandler(selectButton_Click);
selectButton.CausesValidation = false;
nameRowCell.Controls.Add(selectButton);
companyRow.Cells.Add(nameRowCell);
tblCompanies.Rows.Add(companyRow);
}
}
void selectButton_Click(object sender, EventArgs e) // <- Not getting handled
{
Button btn = (Button)sender;
string id = btn.CommandArgument;
if (id != "" && CompanyFound != null)
CompanyFound(new Company() { Id = int.Parse(id) });
}
I've been playing with your code and it does put a button on the screen. The browser source shows the button in the form of an input type submit tag. To test that it is in fact firing add a div to your page somewhere, maybe set it's id to testDiv. Then in your selectButton_Click event hander add a line to the effect of: testDiv.innerHtml = "made it here".
If the div changes, your button fired.
Also, maybe add protected, public or something before the void on the selectButton_Click.
I have created a textbox programatically in page load as using the below code:
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell1 = new HtmlTableCell();
HtmlTableCell cell2 = new HtmlTableCell();
cell1.Controls.Add(new Label() { ID = LableID1, Text = Name });
cell2.Controls.Add(new TextBox() { ID = TextBoxID1 });
row.Cells.Add(cell1);
row.Cells.Add(cell2);
dynamictable.Rows.Add(row);
And In the button click event i am trying to get the value from the Textbox and assign that value to anohter TextBox which is statically created as below:
string id = TextBoxID1
TextBox tb = (TextBox)dynamictable.FindControl(id);
string valuetext = tb.Text;
TextBox1.Text = valuetext;
Am getting Object Reference Error, I mean, i am not able to Find the control and create the TextBox.
I have tried to create the TextBox as below method also:
TextBox tb = (TextBox)form1.FindControl(id);
TextBox tb = (TextBox)this.form1.FindControl(id);
TextBox tb = (TextBox)page.FindControl(id);
Any help would be highly helpful for me.
I think you might need to find the row and then the cell and then find the textbox.
Means inpite of doing this:
TextBox tb = (TextBox)dynamictable.FindControl(id);
You need to find the specific row first like
// find by it or index etc
HtmlTableRow tb = (HtmlTableRow)dynamictable.FindControl(id);
// Then find the Table cell and then find textbox..
I hope this will help you
In order to work with dynamic controls you need to fully understand the ASP.Net Page Life-cycle
Dynamic controls need to be recreated on each post, there's no magic behind responsible of creating your dynamic controls for you, sadly you have to create them explicitly.
Remember that a page is simply a class that is created when you perform a request and destroyed when the response is sent back to the user. Therefore controls need to be recreated every time. This is done for you when the controls are statically declared on the page. But with dynamic controls, you need to recreate them on each post
The code provided by #BobTodd is a good start point, however the recommendation from Microsoft is that dynamic controls should be created in the Page_Init event in order to sync their events with the rest of the static controls.
So your code would look like:
protected void Page_Init(object sender, EventArgs e)
{
CreateTable();
}
Now, remember this simple rule of dumb, when working with dynamic controls, use always the same ID. This is really important because the page viewstate is loaded back based on the control's ID.
Another thing to consider is that all controls created in the Init event, won't load their viewstate until the LoadViewState method is called on every control on the page. This means that if you subscribe to the Page_PreLoad or Page_Load events, you can safely set the control's properties since their value were already loaded from the viewstate and therefore your new values won't be overridden.
This means that any property assigned to the controls before the PreLoad event, will be replaced by the page viewstate value. Therefore is considered a good practice to set your dynamic controls properties after the viewstate has been loaded.
As a quick view, check the ASP.Net page life-cycle:
You might have a method that creates the table, you need to call it on postback to ensure everything is setup.
protected HtmlTable dynamictable;
protected TextBox tb = new TextBox();
protected override void OnInit(EventArgs args)
{
base.OnInit(args);
CreateTableRows();
}
private void CreateTableRows()
{
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell1 = new HtmlTableCell();
HtmlTableCell cell2 = new HtmlTableCell();
cell1.Controls.Add(new Label() { ID = LableID1, Text = Name });
cell2.Controls.Add(tb });
row.Cells.Add(cell1);
row.Cells.Add(cell2);
dynamictable.Rows.Add(row);
}
protected void OnClick(object sender, EventArgs args)
{
return tb.Text;
}
use hidden field to store value of dynamically created text box in java script
also add runat="server" in hidden feild
and you can access your textbox value from hidden feild.
Another way is to store that value in query string using javascript and then reading from it at back end
I am trying to create a UI that creates rows that can be dynamically populated.
I am at the point where I can add a Panel to my list that contains a DropDownList and has an associated Remove Button.
The Remove Button has an OnClick event bound that allows me to remove that specific panel.
I am having an issue when trying to bind a SelectedIndexChanged EventHandler to my DropDownList it does not.
I suspect this had something to do with how I am recreating my controls after every Postback.
I am not asking for or expecting a straightforward "Add this or Change this in the code." I really want to have a understand where I am going wrong, sorry for asking so much! :s
protected void Page_Load(object sender, EventArgs e)
{
//Showing this for the the poster that asked.
if (!IsPostBack)
{
GetClustersFromDB(user);
BindGrid();
BindState();
}
if (Session["persistControls"] != null)
{
persistControls = (List<Panel>)Session["persistControls"];
int count = 0;
foreach (Panel dynamicControl in persistControls)
{
DropDownList list = new DropDownList();
list.ID = "list" + count;
list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged);
list.AutoPostBack = true;
list.Items.Add(new ListItem("", "0"));
list.Items.Add(new ListItem("Title", "1"));
dynamicControl.ID = "panel" + count;
Button btnRemove = new Button();
btnRemove.Click += new EventHandler(btnDelete_Click);
btnRemove.Text = "Remove";
btnRemove.CommandArgument = count.ToString();
myPlaceholder.Controls.Add(dynamicControl);
myPlaceholder.Controls.Add(btnRemove);
count++;
}
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
try
{
DropDownList list = new DropDownList();
list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged);
list.AutoPostBack = true;
list.Items.Add(new ListItem("", "0"));
list.Items.Add(new ListItem("Title", "1"));
Panel panelContainer = new Panel();
panelContainer.ID = "panel" + persistControls.Count;
panelContainer.Controls.Add(list);
Button btnRemove = new Button();
btnRemove.Click += new EventHandler(btnDelete_Click);
btnRemove.Text = "Remove";
btnRemove.CommandArgument = persistControls.Count.ToString();
myPlaceholder.Controls.Add(panelContainer); // Pushes the Panel to the page.
persistControls.Add(panelContainer);// Adds our Panel to the Control list
myPlaceholder.Controls.Add(btnRemove); // Pushes our Button to the page.
Session["persistControls"] = persistControls; // put it in the session
}
catch
{
throw;
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
try
{
int deleteThisOne = int.Parse(((Button)sender).CommandArgument);
persistControls.Remove(persistControls[deleteThisOne]);
Session["persistControls"] = persistControls;
Response.Redirect(Request.Url.ToString());
}
catch
{
throw;
}
}
protected void list_SelectedIndexChanged(object sender, EventArgs e)
{
throw new NotImplementedException();
}
//aspx File Snippets
<asp:Button ID="btnAdd" runat="server" Text="Add Control" onclick="btnAdd_Click" />
<asp:Button ID="btnClear" runat="server" Text="Reset" onclick="btnClear_Click"/>
<asp:PlaceHolder ID="myPlaceholder" runat="server"></asp:PlaceHolder>
Maintaining controls like this is a real pain. You could try an alternative approach:
Create a (small) class that encapsulates the data you want to display in each Panel.
Maintain a List of these objects in Session.
Use an <asp:Repeater> to display the items by data-binding it to your list.
In the repeater's ItemTemplate, you can write out your <asp:Panel> that can contain a <asp:DropDownList>. Use the ItemCommand property of the drop-down list to bind it to a server-side event handler. Similarly you can put an <asp:Button> in the template and bind the ItemCommand property of this to another server-side event handler.
When you click Add, you'll postback to the server, add to your list, save it in session, re-bind the repeater.
When you click a Remove, you'll postback to the server, remove the item from your list, save it in session, re-bind the repeater.
This way lets ASP.Net handle all the control creation for you. However, if you really want to do all that yourself, then this Infinities Loop blog post is required reading.
I don't think this will work. If you create the dropdownlists in the PageLoad event, the viewstate will never get bound to it, and you will never see the option selected by the user.
You should create the controls OnInit, then when the PostBack occurs the ViewState data will get bound to the control and you will be able to access it.