asp:ListView set SelectMethod with a method having parameter - c#

I have a Listview controls like this
<asp:ListView ID="categoryList" ItemType="CodeCamper.EntityLayer.Transaction.FavoriteVO" DataKeyNames="FavoriteID" GroupItemCount="1" SelectMethod="GetCategories" runat="server">
.....
</asp:ListView>
and in the code behind I'm setting the SelectMethod property
categoryList.SelectMethod = "GetbyTime";
Now if I want to call a parameterized method
public List<FavoriteVO> GetbyTime(string message)
{
...
}
How do I have to modify and assign to categoryList.SelectMethod = section ?

It looks like you want to be able to change the SelectMethod to whatever you want. Because of this, I would remove SelectMethod="GetCategories" from the control declaration and set that in codebehind as well. At least that's where I would start.
Then it will be up to you to set the categoryList.SelectMethod in the proper places in code. e.g. you probably want categoryList.SelectMethod = "GetCategories"; in if (IsPostBack) {} and then categoryList.SelectMethod = "GetbyTime"; within some other event or method, like from a button click or whatnot.

It is VITAL, I repeat VITAL to set EnableViewState="False" on the html declaration of your usercontrol, if you are hosting an asp:ListView inside an ascx control, inside an aspx control!
I wasted days of development time because of this.

Related

Set a property of a custom user control in the code behind

I found this question that shows properties on a custom user control (ascx) can be assigned inline as an HTML attribute: Custom attribute in UserControl (*.ascx)?
This works great but what about if I register a custom user control on my page and want to set/get attributes from that control in my code behind?
ASPX:
<%-- I can assign ActivePage inline and this works fine --%>
<wd:NavBar ID="MyNavBar" runat="server" ActivePage="navbarItem1" />
ASPX.CS:
// I need to change the ActivePage
if (what == "internal")
{
RunInternalComparison();
MyNavBar.ActivePage = "navbarItem1";
}
else if (what == "external")
{
RunExternalComparison();
MyNavBar.ActivePage = "navbarItem2";
}
That's what I want to do but it doesn't work. Is this possible?
Do'h! This actually works. I guess Visual Studio wasn't auto generating controls. Simply adding protected NavBar MyNavBar; to the top of my Page solved the problem. I hope someone else finds this useful.

How can access property of .aspx in the user control?

I have an .aspx page in which I have a property. Now i create a user control and drop it on the page. Now how can i access that property in code behind of usercontrol.
The best way is to expose a public property in the UserControl and assign that property from the ASPX page:
By code:
var uc = this.myUserControl as MyCuserControlType;
uc.CustomUserControlProperty = this.MyPageProperty;
Declaratively
<uc:MyUserControlType1 runat="server= ID="myUserControl" CustomUserControlProperty="<%# this.MyPageProperty %>" />
Note: If you want to use declarative markup you would need to call this.DataBind(); in code to force the binding
Edit 1
In case you want to do the opposite (pass a value from the control to the page in response to an event) you could declare your own custom event in the user control and fire it when needed it.
Example:
User control code behind*
public event Action<string> MyCustomEvent = delegate{};
....
// somewhere in your code
this.MyCustomEvent("some vlaue to pass to the page");
Page markup
<uc:MyUserControl1 runat="server" onMyCustomEvent="handleCustomEvent" />
Page code behind
public void handleCustomEvent(string value)
{
// here on the page handle the value passed from the user control
this.myLabel.Text = value;
// which prints: "some vlaue to pass to the page"
}
If a user control needs to access something on the parent page, then maybe this user control should have included it as its own property which could be set from the parent. Ideally user controls should be independent from any parent context or other user controls on the page, otherwise they really are not something reusable. They need to be self contained and configurable throughout their the properties they are exposing.

ASP.NET Declarative binding for Hyperlink Visibility not working

I have a hyperlink in a usercontrol that I'd like to set visibility by using a declarative property, i.e.
<asp:HyperLink ImageUrl="/images/icons/rss.png" Visible="<%# ShowRssIcon %>" ID="FeedHyperLink" runat="server"></asp:HyperLink>
However, it always remains visible, even if ShowRssIcon is false. ShowRssIcon is a simple property set on the usercontrol. Even setting ShowRssIcon to always return false results in the hyperlink showing.
However, setting Visible="false" or Visible="true" manually works as expected. Also, setting the property in the code behind on Page_Load event also works.
Any ideas? Thanks.
Since <%# expressions are evaluated at DataBind() time, if you used that, then you need to call DataBind(); method at PreRenderComplete like..
protected void Page_PreRenderComplete(object sender, EventArgs e)
{
DataBind();
}
Your snippet doesn't show any call to DataBind so are you sure there is one? BTW if this hyperlink is not in a databound control like ListView or GridView it is far better to set the property from the code behind.

Label not accessible in code view

I did type runat="server" in the label tag. its still not accessible.
I did copy this label from another webform. I've noticed when copying labels from others webforms, sometimes they are not accessible. What is the problem?
Check your designer code and see if its in there. If its not your markup and designer are out of sync unless of course you have the control in a template. I have ran into this issue recently and fixed it by just adding a literal control forcing the designer to regen and then deleting the literal.
from what you have given here, I see you typed runat=server without quotations.
try adding quotations and check again.
runat="server"
full example
<asp:label runat="server" ID="Label1" >Label1</asp:Label>
It's because your code behind class is missing reference to that control. You guess you dont have .designer with your page class, right? Then you have to "map" that control manually
YOu can definie class variable like Label myLabel and then later in Page_Load you have to use myLabel = Find('myLabelId') function, to map that label. (This might not be 100% accurate syntax).
Edit: Asuming your label has ID="Label2", code should look like:
Label _label2;
Page_Load(
// some code
_label2 = (Label)FindControl("Label2");
)

Change the Access Modifiers of ASP.NET controls

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.

Categories