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.
Related
I'm setting up a web application via ASP.NET, and I want my links to change a specific color for a specific amount of time once they've been used. How might I go about doing this?
I haven't tested any of this out yet. I don't know exactly where to begin.
So say on my web application I have a link on my home page. It's default color is black. I want to be able to click on the link, have it bring me to the corresponding linked page. This linked page allows me to edit fields, add comments, etc. I want any edits/comments to trigger a specific change, such that when I go back to my homepage, the link is a different color, say green.
think of the color change as a type of "read" feature for emails. If I have multiple links, I want to know which ones I have visited and made changed to. this is different than the hyperlink color change where it turns a different color once clicked, since this isn't a permanent change, and the link could return to the default color if the page has been refreshed.
Any ideas?
Edit1: The infrastructure for this problem was passed onto me by someone else, and it would be hard to move this to another language type.
Edit2: I currently have the following code pertaining to this one link:
<body>"Identification number: #Model.id</body>
where "#Model.id" is declared in the following c# code
namespace WebApplication1.Pages
{
public class IndexModel : PageModel
{
public string time { get; set; }
public int id = 12345;
public void OnGet()
{
time = DateTime.Now.ToShortTimeString();
}
}
}
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.
Is it possible to loop through list of models and update displayed data in view without refreshing the page or making ajax call to server?
Scenario:
Model:
public SomeModel
{
public int Id { get; set; }
public string LinkName { get; set; }
public string ItemDecsiption { get; set; }
public string Text { get; set; }
}
List of SomeModel objects is inicialized, filled with data and passed to View in controller ActionResult. I can see all data in view (i am able to loop through individual models and create list from SomeModel.LinkName property.
<ul>
<% foreach (SomeNamespace.SomeModel m in Model)
{ %>
<li class="green"><%= m.LinkName %></li>
<% } %>
</ul>
What I want to do is to divide page content section into two parts-one with menu (consisting of links created from every LinkName in Model) and second containing block with description and text (for id currently clicked in menu). Now comes the trick part. I want to be able to change displayed data in second block after clicking Link in menu without refreshing the page or making ajax call to server (since all the data I need is already available to client side). Only solution I could think of is to generate hidden blocks for every SomeModel object and then write jquery to manipulate visibility after link click. What I want to know is if there is some more elegant way to accomplish this.
Project is written in C#.NET 3.5 ASP.NET MVC 2.0
The only way to do this would be to use JavaScript to manipulate the DOM.
Depending on the markup which you'll produce, hidden html blocks may be the most elegant solution of all. Like, for intance, if you switch visibility by simply changing a class of block which should be visible without changing the DOM.
The other option will be to prepare json object from Model and use it as data source for visible markup. But this may lead to raise of complexity which is not necessary in most of the cases.
there is another way.
1.var data=#Model.ToJson() in js; there is not a ToJson function ,that just what you need to do
2.only one content block
3.when click menu,find element in data,and set block content use js
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.
I'm having a Repeater used as a sort of Paging TagCloud. To do so, I've added simple properties to the Page's ViewState such as a Page, RowCount, etc...
I feel like it doesn't belong there but I had bad experiences with server controls, debugging, dll and deployment.
Could I just inherit the Repeater class, add a few ControlState/ViewState properties and be able to use it exactly as a Repeater dragged straight from the ToolBox?
Here, having the following simple class:
public class TagCloud : Repeater
{
public int selectedIndex;
public TagCloud()
{
selectedIndex = -1;
//
// TODO: Add constructor logic here
//
}
public int SelectedIndex
{
get { return selectedIndex; }
set { selectedIndex = value; }
}
}
Without creating a new WebControlLibrary project, could this cs file stands in the App_Code folder and work like expected?
Thanks.
Yes, there is no problem doing this. As long as you use the control's property syntax:
public int RowCount
{
get { return (int) (ViewState["RowCount"] ?? 0); }
set { ViewState["RowCount"] = value; }
}
Also, to make your properties look the same as the default ones you can add Description or DefaultValue attributes.
This works, and is one suggested way of building server controls. Try it and see.
Inheriting from the ASP Repeater is a completely valid approach, so long as the control you are building IS a repeater with additional properties.
If you feel the control you need is actually "something else" that happens to have a repeater as part of its control set, then you likely need to make a composite control which adds a repeater to its control collection, along with whatever other controls are needed.
For example, you might want to have a control that has a repeater, a label of search results, links to go to the top and the bottom of the repeater's contents, etc. This composite control is not a repeater, but does use a repeater.