I've created a Table User control. In each cell in table, there are checkboxes. How can I access the attributes of selected checkboxes in the default.aspx page.
I've dragged Table user control into default.aspx
<uc1:SchTable ID="SchTime1" runat="server" />
Am relatively new to User Control. Was trying it out because of maintainability.
I managed to get the codes to work by hard coding the table (not using user control) on the same page as default.aspx though
Add a property to the UserControl that accesses and returns the data you want.
In SchTable you can add any number of public properties and methods you want. Some examples:
public IEnumerable<ListItem> SelectedItems
{
get
{
return ACheckboxList.Items.Cast<ListItem>().Where(i => i.Selected);
}
}
public IEnumerable<Checkbox> GetAllCheckboxes()
{
//Find and return the checkboxes here just like you would in the page
}
And then in the Default page, you can access that information:
var selected = SchTime1.SelectedItems;
var checkboxes = SchTime1.GetAllCheckboxes();
There is a MSDN tutorial here that goes more into details on all this.
Related
I have a page that have the following code for a custom control:
<SiteControls:Announcements runat="server" id="UserAnnouncements" />
Let's also say I have a GridView control, just so I can cover multiple scenarios. I need to check if the user has permission to view this control by checking the Boolean:
PermissionsManagement.DoesUserHavePermission(userId, permissionId)
Which is defined as:
public static class PermissionsManagement
{
public static bool DoesUserHavePermission(int userAccountId, int permissionId)
{
// Code Goes Here
}
}
If the user doesn't have permission, DoesUserHavePermission will return false. I have the ASP.NET WebForms page laid out as if the user has full control (meaning I have all the controls on the page and want to remove them if they don't have permission vs adding every single control to the page).
I can set the control's visibility to false on Page_Load function if the user doesn't have permission, but that doesn't stop my control from loading or in the case of a GridView from loading its data. How do I stop a control (User control or standard control) from loading any data if the user doesn't have permission to use (view) the control? I have tried the following inline code which doesn't work:
<% if(PermissionsManagement.DoesUserHavePermission(1, 1))
{ %>
<SiteControls:Announcements runat="server" id="UserAnnouncements" />
<% } %>
But that doesn't work as the control Page_Load still fires for the control and I assume any other control will load data if it is data-bound or acts similar to my control.
Without knowing much of your code, it is a little difficult to figure out the exact answer. However, as much as I understood your question, here's my answer.
Loading data for Announcements or GridView should still be in your control. I would expose a method in Announcements control that actually loads data for it. For the GridView you should simply defer the binding of DataSource until the permission check is performed. Of course these things need to be done in addition to hiding (setting visibility) of these controls.
See the code below, not complete, but enough to express an idea:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Check permissions here
if (allowed)
{
// For custom/user control
UserAnnouncements.GetAnnouncements();
// For grid view
GridView1.DataSource = GetGridviewData(); // GetGridviewData would return DataSet or anything valid.
GridView1.DataBind();
}
else
{
// Hide the controls
}
}
}
ASP.NET 4.0. I want to allow users to view/edit items on a customer's license. A license has 1-M products, and the list of products will expand over time, so I'm dynamically creating a list of all products (in Page_Load) with a checkbox to say whether a license has that product, like this:
CheckBox cbxProduct = new CheckBox();
cbxProduct.ID = "cbxProduct" + product.ID.ToString();
I can find those dynamic controls and access their values on PostBack using:
CheckBox cbxProd = (CheckBox)pnlLicenseDetails.FindControl("cbxProduct" + productID.ToString());
but only if they have just been re-created in Page_Load (or Page_Init, doesn't seem to matter). The problem is that I want the user to be able to uncheck a box to remove a product from the license and then save the results. But in order to find the checkbox and determine its value, I have to re-create the controls, which of course erases any value the user entered.
If I try to reference the checkboxes without re-creating them, I of course get an "object reference not set..." error. So I need some way to capture the values the user inputs before they are wiped out.
Suggestions?
You have to create dynamic controls in Page_Init. You have to create them the same way for all requests. That means, run the same code to create the control and add it to the controls collection every time the page is loaded regardless of IsPostBack or not.
Also, I would suggest that you save the instance of the dynamic control to a private member variable so you don't have to call FindControl as that is potentially expensive.
Since it looks like you have a list of products somewhere, here is an example using a dictionary to store the checkboxes:
public partial class _Default : Page
{
private Dictionary<Int32,CheckBox> _myDynamicCheckBoxes;
protected override void OnInit(EventArgs e)
{
_myDynamicCheckBoxes = new Dictionary<Int32,CheckBox>();
foreach (var product in _listOfProducts)
{
var chkBox = new CheckBox {ID = "CheckBox" + product.ID.ToString()};
_myDynamicCheckBoxes.Add(product.ID,chkBox);
//add the checkbox to a Controls collection
}
}
}
Then somewhere else in your code, when you have a product and you want to find the checkbox associated you can use: var aCheckBox = _myDynamicCheckBoxes[product.ID];
I have a webuser control having repeater control inside it like below.
<asp:Repeater ID="repeaterInvoicesPaid" runat="server">
</asp:Repeater>
I just dragged it to my data.aspx page.Now i have a method inside data.cs C# file which is returning the data from table .i want to ask ,how to bind the repeater(which is inside the user control)?
it will be binded inside data.cs file or web user control's own C# file.?
And please tell me how to access the repeater id from user control inside data.cs file?
Thanks in advance.
As for question 1: You can either
expose the repeater (see question 3) or
expose the datasource
Personally I am in favour of the last option
For databinding options, you can choose to expose the built-in databind method or you can databind when the source is set.
Exposing the datasource could be done like this
public static object RepeaterDataSource {
get { return repeater.DataSource; }
set { repeater.DataSource = value; }
}
or make a method to set it, to allow manipulation upon setting, like databinding.
Question 2: The actual binding always happens where the repeater is. If you need an OnItemDataBound handler, then that one will be in the usercontrol's code-behind, regardless of where you bind it from. You can however expose that too, but I see no reason to do so.
Question 3: If you want the id, then I assume the client id. You can get that with something like this
public static string RepeaterClientID {
get { return theRepeater.ClientID; }
}
Although I'm not sure that's what you actually mean. If you want a reference instead, then
public static Repeater TheRepeater {
get { return theRepeater; }
set { theRepeater = value; }
}
Lastly, accept more answers to your questions, or delete them entirely. Your acceptance rate is very low.
Create object of user control
UserControl usr = Page.FindControl("usercontrolID");
Repeator rept = usr.FindControl("repeatorControlID");
rept.DataScourse= Datatbale;
rept.DataBind();
You could always make a public property in your user control that returns the repeater.
As the title says...
If I start my app project with a pivot page(MainPage.xaml) and then choose to click for example the "design two" link in the databinded listbox. Is it possible to bind the "LineThree" text for the "design two" link in to a separate portrait page?
Do I have to make new portrait page for every "LineThree"-link? Or can I just generate the "MainViewModelSampleData.xaml" data to a single portrait page depending on what "LineOne"-link I click in the pivot page in the start?
Hope my question is understandable... :P
If I understand you correctly, you want to have a main page that contains a list of data, and then a details page whose contents are dependent on the item that you clicked in the main page. The answer to your question is then "yes". There are a number of ways to achieve this, some of which include global variables, a custom navigation service, storing a value in isolated storage and so on. My personal preference is to use the context of the NavigationService and to pass an ID or an index in the query string for the target page.
Your call to navigate to the details page then looks like this:
Application.Current.Navigate(string.Format("/Views/DetailsView.xaml?id={0}", id));
In the target page, you override the OnNavigatedTo handler to retrieve the value that you passed and then process it accordingly (i.e. look up the value from your database, or retrieve it from a data collection).
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (this.NavigationContext.QueryString.ContainsKey("id"))
{
var id = this.NavigationContext.QueryString["id"];
// TODO: Do what you need to with the ID.
}
else
{
// I use this condition to handle creating new items.
}
}
How is what you're trying to do different from what is created by default in a new DataBound Application? That lets you select an item in the list on the main page and then displays another page which includes the text from LineThree.
I suggest you look at the sample code created as part of a new DataBound Application.
I have written a user control that contains a text box and a button. Functionality is such that you input a QuoteID into the textbox and click the button then the app goes and fetches a bunch of information about the specified quote and plops it onto the screen.
My employer wants this to be modified to be only a button which will reside on the page that displays the order normally. So, obviously on this page we have access to the QuoteID. However, since the text-box is no longer part of the control I'm not sure how to get to the ID.
Is there any way to pass information into a custom control? Perhaps a way to set up the HTML so that the control knows what ID it will be searching for if clicked?
We are not using MVC.
You can add a public property to the user control for the QuoteID and set it whenever you know what it is.
Example:
//code behind of your user control
class SpecialUserControl: UserControl
{
//this property is now accessible outside this user control
public int QuoteID { get; set; }
}
You could always save the ID as a query string in the url, then retrieve it in the code behind file. It should then be accessible from the HTML. This would depend on how sensitive the ID was though, as it would be viewable in the address bar.
string ID = Request.QueryString[1];
That would help with the extraction process, and from there simply pass the ID to wherever its needed in the code behind file.
A public property in the user control is the way to go.
public string QuoteID { get; set; }
You can assign it from the page, or from the code behind.
<uc1:QuoteControl id="QuoteControl1" runat="server" QuoteID="myquoteid" />
or
QuoteControl1.QuoteID = "myquoteid";
Just add a propert in the user control:
public string MyQuote
{
get
{
return txtQuote.Text;
}
set
{
txtQuote.Text = value;
}
}
Then you can access it from your page as:
<uc1:Quote id="QuoteUserControl" runat="server" />
string quote = QuoteUserControl.MyQuote;
asp:HiddenField controls might work out for you here.
In your page load method, you can pass the property to the user control - something like this example (where we set a hidden label to be the quote id for use when you press the button...)
Control c = LoadControl("QuoteSearch.ascx");
Label hiddenLabel = (Label)c.FindControl("HiddenQuoteIdLabelOrWhatever");
hiddenLabel .Text = "32415";
Although, you have to ask yourself whether a user control is really any use at this stage as it doesn't sound like the nice re-usable, stand-alone quote search box that you originally designed.