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.
Related
A little new to asp.net.
In my main.aspx page i have:
<users:UsersControl runat="server" ID="usersControl" />
In my UserControl page_load i have:
ViewState["test"] = "test";
In my Page_PreRender in main.aspx.cs:
log...(ViewState["test"]); <-- empty
Why dont i see the value on test?
Im guessing here that the ViewState collection is different in the two contexts you have mentioned.
The first is in the context of the control, and the second is in the context of the page, therefore "test" key is not shared between them.
Also, it is not a good idea to expose a controls ViewState beyond the control boundary. For example use properties on the UserControl as the interface to the viewstate, e.g
public string Test
{
get { return this.ViewState["Test"]; }
set { this.ViewState["Test"] = value; }
}
ViewState should be considered an internal implementation detail of the user control.
Then whenever you need to use this property from the page:
this.userControl1.Test = "This Goes Into ViewState";
I've found a similar answer to your question:
.net ViewState in page lifecycle
It's necessary to understand the life cycle, so why don't use Attributes on the UserControl?
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.
If I put a control in a .aspx file like this;
<asp:TextBox ID="protectedTextBox" runat="server">Some info</asp:TextBox>
I get a declared control in the page's .aspx.designer.cs file;
protected global::System.Web.UI.WebControls.TextBox protectedTextBox;
But I'd like to change the access modifier of the control to public. Is there any attribute or similar that I can set to change the access modifier?
Here's why I want to do it. I am trying to have cross-page postbacks work nice and neatly. I have two pages:
FirstPage.aspx
MyTextBox : textbox
MyButton : button, #PostbackUrl=Secondpage
SecondPage.aspx
MyLabel : label
When the user clicks FirstPage.MyButton, I want to write the value of FirstPage.MyTextBox.Text into SecondPage.MyLabel.Text. I could do it with Page.FindControl, but this seems like a poor substitute to casting the previous page as a FirstPage object and referring directly to the MyTextBox control on it. Something like this;
// on the page_load of SecondPage.aspx;
var previousPage = this.PreviousPage as FirstPage;
this.MyLabel.Text = previousPage.MyTextBox.Text;
Is there any way to change the access modifier?
You can just delete the declaration from the designer and put it in your code behind.
The comments around the declaration say to do this.
/// To modify move field declaration from designer file to code-behind file.
One option I've considered is writing a public property which exposes the original page;
public TextBox PublicTextBox { get { return this.MyTextBox; } }
Which would get the job done, but seems hacky.
Steve, exposing that page's controls would make sense if you'd need to manipulate those controls, but in your case you just need to pass some data (that string) to the other handler, so I would expose that and not the control itself.
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.
Is it possible to access an element on a Master page from the page loaded within the ContentPlaceHolder for the master?
I have a ListView that lists people's names in a navigation area on the Master page. I would like to update the ListView after a person has been added to the table that the ListView is data bound to. The ListView currently does not update it's values until the cache is reloaded. We have found that just re-running the ListView.DataBind() will update a listview's contents. We have not been able to run the ListView.DataBind() on a page that uses the Master page.
Below is a sample of what I wanted to do but a compiler error says
"PeopleListView does not exist in the current context"
GIS.master - Where ListView resides
...<asp:ListView ID="PeopleListView"...
GISInput_People.aspx - Uses GIS.master as it's master page
GISInput_People.aspx.cs
AddNewPerson()
{
// Add person to table
....
// Update Person List
PeopleListView.DataBind();
...
}
What would be the best way to resolve an issue like this in C# .Net?
I believe you could do this by using this.Master.FindControl or something similar, but you probably shouldn't - it requires the content page to know too much about the structure of the master page.
I would suggest another method, such as firing an event in the content area that the master could listen for and re-bind when fired.
Assuming the control is called "PeopleListView" on the master page
ListView peopleListView = (ListView)this.Master.FindControl("PeopleListView");
peopleListView.DataSource = [whatever];
peopleListView.DataBind();
But #palmsey is more correct, especially if your page could have the possibility of more than one master page. Decouple them and use an event.
Option 1 :you can create public property of your master page control
public TextBox PropMasterTextBox1
{
get { return txtMasterBox1; }
set { txtMasterBox1 = value; }
}
access it on content page like
Master.PropMasterTextBox1.Text="SomeString";
Option 2:
on Master page:
public string SetMasterTextBox1Text
{
get { return txtMasterBox1.Text; }
set { txtMasterBox1.Text = value; }
}
on Content Page:
Master.SetMasterTextBox1Text="someText";
option 3 :
you can create some public method that works for you
these approach is not so useful but it helps if you just want to use some limited and predefined control
One think to remember is the following ASP.NET directive.
<%# MasterType attribute="value" [attribute="value"...] %>
MSDN Reference
It will help you when referencing this.Master by creating a strongly typed reference to the master page. You can then reference your ListView without needing to CAST.
you can access with the code this.Master.FindControl(ControlID) which control you wish. It returns the reference of the control, so that the changes are effective. about firing an event could not be possible each situation.
Assuming your master page was named MyMaster:
(Master as MyMaster).PeopleListView.DataBind();
Edit: since PeopleListView will be declared protected by default, you will either need to change this to public, or create a public property wrapper so that you can access it from your page.