I have created a usercontrol to capture education details, it contains 5 textboxes and an functionto insert that values into my db. I have added the usercontrol 5 times to a page. I have a button on my aspx page which I want to be able to click and call the function to insert the values.
Can anyone suggest how I can do this?
The function which inserts values into your db should not be in your usercontrols but in your page. Does your usercontrol has a save button itself or only the page? On first variant you should raise an event and the page could catch it to save the values. On second variant you should iterate through your usercontrols(added dynamically or fixed?). You should add public properties which return the textbox's values to the page, then you can save these values into the db.
Put a public method on your user control.
// Inside MyUserControl
public void SaveIt() {
// logic to perist the values to db.
}
From your ASPX page (or code behind) when the button is clicked, loop over your user control instances and call the method:
// On Click...
MyUserControl[] arrControls = new MyUserControl[] {myUC1, myUC2, myUC3, myUC4, myUC5};
foreach (MyUserControl c in arrControls)
c.SaveIt();
Related
I have a dynamically created bill of material with each line item being a dynamic user_control. the user_control has a textbox for quantity entry. When I click the submit button i'd like to get all the quantities from each textbox but the controls have all disappeard on the page load and the page shows zero controls.
I know you can turn on autopostback for the textbox then catch each individual text_changed_event but that doesn't seem efficient. I'd like to just loop through all of them when user clicks submit button, then take them back to the same bill of material page.
First of all the reason why controls disappear on postback is that they were added to your page dynamically and when postback occurred the information about the dynamic controls were lost and page had no information about these dynamic controls.
Now about getting the values from controls inside dynamic user controls, you have use the FindControl method or have to iterate through the Controls collection of user control to get the reference to TextBoxes.
An idea about how to do this:
//1. Using ID of user control
//inside button_click method
protected void btnSubmit_Click(...)
{
TextBox txt1 = idOfUserControl.FindControl(textBoxId);
}
//2. Using type of user control
//inside button_click method
protected void btnSubmit_Click(...)
{
foreach(Control c in Page.Controls)
if(c is YourUserControlClass)
{
YourUserControlClass control = (YourUserControlClass)c;
TextBox txt1 = c.FindControl(textBoxId);
}
}
In an ASP.NET page I have added two user control with a dropdownlist each one. The selection of a dropdownlist should be changed the query to the second user control.
What is the most efficient way to pass the selected value of dropdownlist to the second?
I initially thought of creating a public event (public string OnClientSelectedIndexChanged) while the first user control and outsource through a public string SelectedValue the selected value of the second user control:
public string SelectedValue
{
get
{
rcg.SelectedValue return;
}
set
{
rcg.SelectedValue = value;
}
}
rcg is the dropdownlist.
Could be corrected or have alternatives?
Thanks in advance
Alternative is maybe to use AJAX.
Surround the two dropdownlist with an ASP update panel and trigger this one with the first dropdownlist 'OnSelectedIndexChanged'. In the event's code you can bind your second dropdownlist..
Like this it is not necessary to reload the entire page for each first dropdownlist change..
Yes, you are on right track.
1: You expose an event from UC1(e.g. uc1ddlchanged) and a property from UC2 (e.g. uc1ddlSelectedValue).
2: In the OnSelectedIndexChanged of ddl1 in UC1 your raise the event uc1ddlchanged.
3: On your page that has UC1 and UC2 you handle this event and set the UC2.uc1ddlSelectedValue = UC1.SelectedValue
4: In the UC2 -> uc1ddlSelectedValue -> set{} you can set the value and rebind your ddl and or do any other thing needed to update the UC2.
I have some updatepanels in asp.net page. I want updatepanel 2 get refreshed when updatepanel 1 does, but UpdatePanel 2 has his UpdateMode attributed set to Conditional and ChildrenAsTrigger=False, because it has another updatePanels inside and i need to control the way it is refreshed. I was trying to make a trigger for the other updatepanel but this seems not work, maybe i am missing something
Is there a way to make this happen?
If you can't figure a valid example, imagine product category list and product list, when selected category change (linkbutton inside updatepanel) product list is refreshed which is inside another updatepanel, product list change for example when you order by price (order is inside other updatepanel) and i don't want category list gets refreshed.
thanks in advance.
You can call updatePanelProdList.Update() inside the code-behind file of your page. The place to call this method in your scenario would be the event handler for the category change LinkButton.
Thank you, that's what i am looking for! I have 2 "Update Panel" on my page, one keeping my form and other one keeping flexiGrid (non .net version,pure jQuery).
I do manual AsyncPostBack from my grid to get in edit or delete mode.
function doPostBackAsync(eventName, eventArgs) {
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (!Array.contains(prm._asyncPostBackControlIDs, eventName)) {
prm._asyncPostBackControlIDs.push(eventName);
}
if (!Array.contains(prm._asyncPostBackControlClientIDs, eventName)) {
prm._asyncPostBackControlClientIDs.push(eventName);
}
__doPostBack(eventName, eventArgs);
}
<a title="Edit" href="javascript:doPostBackAsync('Edit','2');">Edit</a>
and then i handle it like
string EventName = Request.Form["__EVENTTARGET"].ToString();
int EventValue = Request.Form["__EVENTARGUMENT"].ToString();
after determinate EventName and EventValue calling the method below
protected void Edit(int id)
{
//load the form here
UpdatePanel1.Update(); //do the trick right, without this line unable to show form fields and other stuff with newly loaded data
}
regards.
(sorry for my English)
In PageLoad event of the form, I can not reference server side control within logged in template. What am I missing. So when I am logged in I will show text box control otherwise I will show text like "please login to do soso.."
Please help ..
you can use the FindControl method on your loginview control to get them...
TextBox t = (TextBox)LoginView2.FindControl("TextBox1");
string s = null;
if (t != null)
{
// textbox is in the current scope of the LoginView
s = t.text;
}
else
{
// the textbox is not in the current scope of the LoginView.
}
However, this will only work for the controls that are currently in the shown view of the LoginView control. You'd have to test that you're showing the logged in view before trying to grab the textbox, or you could also test that the FindControl doesn't return a null reference.
If you're still having trouble referencing the hidden object, you might not be entering the right value for it. Say you have a drop down list called "DropDownList1" nested inside a loggedInView. You have to set a new object that uses the FindControl method of the DropDownList class, and then use that NEW object:
DropDownList d = (DropDownList)ucLogin.FindControl("DropDownList1");
bool answer = d.SelectedValue.StartsWith("S");
if (answer == true)
{
Response.Redirect("~/MemberPages/ChangePassword.aspx");
}
In my case, I am redirecting the user to a new page if that objects selected value starts with an "S".
Works for me, and I hope it works for you!
Ben sewards
In one of my ASP.NET Web Applications, I am using a BulkEditGridView (a GridView which allows all rows to be edited at the same time) to implement an order form. In my grid, I have a column which calculates the total for each item (cost x quantity) and a grand total field at the bottom of the page. Currently, however, these fields are only refreshed on every post-back. I need to have these fields updated dynamically so that as users change quantities, the totals and grand total update to reflect the new values. I have attempted to use AJAX solutions to accomplish this, but the asynchronous post-backs interfere with the focus on the page. I imagine that a purely client-side solution exists, and I'm hopeful that someone in the community can share.
If your calculations can be reproduced in JavaScript the easiest method would be using jQuery to get all the items like this:
$("#myGridView input[type='text']").each(function(){
this.change(function(){
updateTotal(this.value);
});
});
Or if your calculations are way too complex to be done in JavaScript (or time restraints prevent it) then an AJAX call to a web service is the best way. Lets say we've got our webservice like this:
[WebMethod, ScriptMethod]
public int UpdateTotal(int currTotal, int changedValue){
// do stuff, then return
}
You'll need some JavaScript to invoke the webservice, you can do it either with jQuery or MS AJAX. I'll show a combo of both, just for fun:
$("#myGridView input[type='text']").each(function(){
this.change(function(){
Sys.Net.WebServiceProxy.invoke(
"/Helpers.asmx",
"UpdateTotal",
false,
{ currTotal: $get('totalField').innerHTML, changedValue: this.value },
showNewTotal
);
});
});
function showNewTotal(res){
$get('totalField').innerHTML = res;
}
Check out this link for full info on the Sys.Net.WebServiceProxy.invoke method: http://www.asp.net/AJAX/Documentation/Live/ClientReference/Sys.Net/WebServiceProxyClass/WebServiceProxyInvokeMethod.aspx
One solution is to build some javascript in you RowDataBound method to constantly update those totals when the textboxes change.
So during the RowDataBound, start building a javascript string in memory that will add up the textboxes you need added. What's nice in RowDataBound is you can get the Client Side id's of these textboxes by calling TextBox.ClientId.
Add this javascript to the page, then also add an onkeyup event to each textbox you require to call this script.
So something like (this is a row bound event from a gridview)
private string _jscript;
protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Get your textbox
Textbox tb = e.Row.FindControl("tbAddUp");
//Add the event that you're going to call to this textbox's attributes
tb.Attributes.Add("onkeyup", "MyAddUpJavaScriptMethod();");
//Build the javascript for the MyAddUpJavaScriptMethod
jscript += "document.getElementById('" + tb.ClientId + '").value + ";
}
}
Then once you've built that whole script, use your Page.ClientScript class to add a method to you page which will be called by your onkeyup in your textboxes "MyAddUpJavaScriptMethod"
Hope that makes sense and helps