Juice UI - Textbox inside accordion issue - asp.net

I'm unable to retrieve the content of a textbox that's inside an accordion panel. My markup is as follows:
<juice:Accordion ID="Accordion1" runat="server">
<juice:AccordionPanel ID="AccordionPanel1" runat="server" Title="Media ID">
<PanelContent>
<asp:Label ID="LabelMediaID" runat="server" Text="Media ID" AssociatedControlID="TextBoxMediaID"></asp:Label>
<asp:TextBox ID="TextBoxMediaID" runat="server"></asp:TextBox>
</PanelContent>
</juice:AccordionPanel>
My server side code is triggered when the user clicks a button:
protected void ButtonSearch_Click(object sender, EventArgs e)
{
// Retrieve controls within accordion panels
TextBox TextBoxMediaID = (TextBox)AccordionPanel1.FindControl("TextBoxMediaID");
string mediaID= "abc";
if (TextBoxMediaID != null)
mediaID= TextBoxMediaID.Text;
I'm able to successfully retrieve my textbox control but when I try to access its Text property it's always empty.
Can someone help me? I'm afraid I'm reasonably new to the world of ASP.NET, Juice etc. Thanks.

It turned out that my problem was caused by having an ASP.NET project that used master pages.
If you're not using master pages then
TextBox TextBox1 = (TextBox)AccordionPanel1.FindControl("TextBox1");
string content= Request.Form[TextBox1.UniqueId];
works perfectly. However, when using a master page with a ContentPlaceHolder then use the following code:
ContentPlaceHolder cph = (ContentPlaceHolder)Master.FindControl("MainContent");
TextBox TextBox1 = (TextBox)AccordionPanel1.FindControl("TextBox1");
string content= Request.Form[cph.UniqueID + "$" + TextBox1.UniqueId];
Hopefully this will save someone a lot of time!

Option 1 - Quick fix
For your page, make ClientIDMode="static" in page directive and then use Request.Form["TextBoxMediaID"] to get value of your text box.
Option 2 - Debug
Go to debug mode and try AccordionPanel1.Controls[0].("TextBoxMediaID"); or AccordionPanel1.Controls[0].Controls[0].("TextBoxMediaID"); because you don't know how many server side controls are there. Simple solution would be to use find control recursive function.

Related

ASP.Net FormsAuthentication Rememberme

I am using forms authentication in a web application using the built-in Login capabilities, and it has been working well.
I would like to set DisplayRememberMe.visible to false depending on certain conditions (e.g. which Server, ip address, etc). Of course I can manually add visible="false" to the markup shown here, but that seems like a poor way to go.
<asp:CheckBox ID="RememberMe" runat="server" />
<asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe"
CssClass="inline" >Keep me logged in</asp:Label>
Also, I can't figure out which asp field has the DisplayRememberMe field.
But more importantly, in the code behind file, I have added LoginUser.DisplayRememberMe = False, but it is ignored, and the label and checkbox are still visible. I have tried adding it to various events like Page.Load, Page.Init, Login_User.Init, Login_User.Prerender, but the checkbox and label are still visible after the page loads.
Am I using the proper call? Where should I place it to be effective?
This is my first post on SO, so please excuse any poor etiquette.
You can change visibility of CheckBox and Label by creating event of login control as
OnLoad="LoginUser_OnLoad"
On .cs page
protected void LoginUser_OnLoad(object sender, EventArgs eventArgs)
{
var login = (System.Web.UI.WebControls.Login)sender;
var checkbox = login.FindControl("RememberMe");
checkbox.Visible = false;
var label = login.FindControl("RememberMeLabel");
label.Visible = false;
}
You can also put your visibility conditions in LoginUser_OnLoad method.

ASP:Net LinkButton control Postback issue

I have an asp.net linkButton (or imageButton) control in my profile.aspx page. I'am checking Request.Querystring("id") in the page below in the code behind.
http: //localhost:42932/profile.aspx?id=1
When I first load the profile page it is not posted back. It is ok!. When I go to another users profile (the same page just the query string is different) using imageButton control with the adddress;
http: //localhost:42932/profile.aspx?id=2
it is posted back. I dont want it to be posted back. But if I go to this page with a regular html input element like
a href = "http: //localhost:42932/profile.aspx?id=2"
it is not posted back. So I want the image button behave like an html input element.
Here is my imageButton;
ASPX:
<asp:ImageButton ID="imgProfile" ImageUrl="images/site/profile1.png" runat="server"/>
.CS
imgProfile.PostBackUrl = "profile.aspx?id=" + Session["userID"];
Edit:
if (!IsPostBack)
{
Session["order"] = 0;
}
This control is in the page load. So it should be !postback with state I mentioned above. Because all the other functions are working when Session["order"] = 0
Make use of OnCLientClick instead of OnClick, so that you only run client side code. Then, sepcify that you return false;
i.e.
<asp:ImageButton ID="imgProfile" ImageUrl="images/site/profile1.png" runat="server" OnClientClick="return false;" />
But, why use a server control, when this can be done with a normal <img .. html control?
Rather than specifying a PostBackUrl I would recommend using Response.Redirect() in the button click event handler:
public void imgProfile_Click(object sender, eventArgs e){
Response.Redirect("profile.aspx?id=" + Session["userID"]);
}
Or alternatively, just use a Hyperlink control and set the NavigateUrl property during Page_Load:
<asp:HyperLink ID="imgProfile" runat="server"><img src="images/site/profile1.png" /></asp:Hyperlink>
imgProfile.NavigateUrl = "profile.aspx?id=" + Session["userID"];

Displaying and hiding parts of the web form freely without limitations in order to avoid ViewState loading problems

The problem
I have a web form and I would like to hide and show some panels. When I say "Panel" I do not mean the Panel control but just a pane in the html (usually represented by a DIV).
Well I want to show and hide this panels as I wish... no limits means that not necessarly only one pane must by visible, also 2, 3 or 4 can be.
Usually I achieved this by:
<div id="mypane1" runat="server">
...
</div>
<div id="mypane2" runat="server">
...
</div>
<div id="mypane3" runat="server">
...
</div>
and this:
this.mypane1.Visible = true;
this.mypane2.Visible = false;
this.mypane3.Visible = true;
Unfortunately these panels contain controls and some of them are not shown from the beginning (first web form load, so when no PostBack happens). This leads to problems for ViewState loading.
Regarding ViewState loading
Of course many will obviously ask about these ViewState problems of mine. Well, briefly, the thing is this: a ViewState loading error is not easy to manage but in the end I could understand the following.
Let's say to have this:
<!-- In .aspx -->
<div id="mypane1" runat="server">
...
<asp:Literal ID="..." runat="server" ...></asp:Literal>
<asp:TextBox ID="..." runat="server" ...></asp:TextBox>
...
</div>
<div id="mypane2" runat="server">
...
<asp:LinkButton ID="lb1" OnClick="lb1_Click" runat="server" ...></asp:LinkButton>
<asp:LinkButton ID="lb2" OnClick="lb2_Click" runat="server" ...></asp:LinkButton>
<asp:LinkButton ID="lb3" OnClick="lb3_Click" runat="server" ...></asp:LinkButton>
...
</div>
<div id="mypane3" runat="server">
...
<asp:TextBox ID="..." runat="server" ...></asp:TextBox>
<asp:LinkButton ID="lb4" OnClick="lb4_Click" runat="server" ...></asp:LinkButton>
<asp:TextBox ID="..." runat="server" ...></asp:TextBox>
...
</div>
// In .aspx.cs
protected void Page_Load(...) {
if (!this.IsPostBack) {
this.mypane1.Visible = true;
this.mypane2.Visible = false;
this.mypane3.Visible = true;
}
}
/// ... In the page many happens and the panes are shown and hidden ... ///
// Event Handlers
protected void lb1_Click(object sender, EventArgs e) {
...
}
protected void lb2_Click(object sender, EventArgs e) {
...
}
protected void lb3_Click(object sender, EventArgs e) {
...
}
protected void lb4_Click(object sender, EventArgs e) {
...
}
Well the problem is the following.
At the beginning pane2 is not shown.
After some actions pane2 is shown and its controls are shown too.
User click lb1 --> ViewState error
Note that the same happens for every event associated to pane2's controls. Is lb4 is pressed, no problems.
The problem is that the beginning hierarchy is represented by controls in pane 1 and 3. When events are raised by controls not part if the beginning hierarchy, ViewState is found inconsistent (because I act on HtmlControls, usually ServerControls are more intelligent and can manage better ViewState when controls are added inside them).
Feasible solutoions
Note that I could simply do the following to hide and show panels:
this.mypane2.Attributes.Add("display", "none");
This is not something I would like to do for two reasons:
I want panes not to be rendered as Html.
I would like to use server controls.
Request
I need to use web controls to manage my panel management, I cannot use HtmlControls because Control Hierarchy is not correctly loaded.
I tried MultiView and View controls but they enable me to use only one active view. How can I do?
Note: Please do not focus too much on the ViewState problem, it is just a good desc to let you know what happens, my only interest is to find a way to hide and show panes freely using server controls.
Thankyou
I'm assuming that setting the visiblity to false for the panels causes the children not to be added to ViewState or interfers with it in some way. Is that correct?
Given that, if you want to have the controls there as far as the .Net rendering engine is concerned but hidden from the end user you could try to show and hide them using css i.e.
show
pnlDemo.Attributes.Add("style", "display:block");
hide
pnlDemo.Attributes.Add("style", "display:none");
I had a situation where I needed ASP.Net controls there for ViewState purpose but not shown. At the appropriate point I would show them using JQuery and this method work for that scenario so it may work for yours.
Have you tried using the ViewStateModeByIdAttribute?
The ViewStateModeByIdAttribute class is used to specify a control that requires view-state loading by ID. The default view-state loading behavior is for ASP.NET to load the view-state information for a control by its index in the control tree of the page. There is a performance cost for loading view-state information by ID because the page control tree must be searched for the control specifically before loading its view-state information.
Have you tried with page.RegisterStartupScript .
script will load at the end of page load so control will have their viewstate .
Write JavaScript code that get the id of panel to hide & change the CSS to visibility hidden not display none.
another asp button on which you want to display the panel.
you can write JavaScript code onClientClick event of button to change the CSS of panel to change its visibility.

Repeater databound loses data & event on postback - is there a best practice solution?

Currently struggling with a problem that I've encountered variations on in the past. At the moment a worthwhile solution escapes me, but it seems such an obvious issue that I can't help wondering whether or not there's a "best practice" approach I should adopt.
Without code, here's the issues in a nutshell:
page has databound control (a repeater) which isn't populated until user inputs data and clicks a button.
Repeater item template contains a button
User clicks button, page posts back. On load, the repeater is actually empty so event is never handled because the originating control no longer exists
Go back to the beginning of wretched cycle
I've confirmed that this is the problem because if you provide the repeater with some static data on page load, everything works fine. But of course that's no use because it has to be populated dynamically.
Is there a commonly approved way round this headache? I can store the data in session and re-use it on page load, but it seems terribly clumsy.
Cheers,
Matt
If the event is being fired by a button within a repeater then this would bubble up to the repeaters ItemCommand event. Using a buttons CommandName and CommandArgument parameters you can then identify which button was clicked and act accordingly. Below is some basic markup and code behind to demonstrate the approach:
HTML:
<asp:Repeater ID="rptTest" runat="server" onitemcommand="rptTest_ItemCommand"
onitemdatabound="rptTest_ItemDataBound">
<ItemTemplate>
<p>
<asp:Button ID="btnTest" runat="server" />
</p>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="btnLoad" runat="server" Text="Load" onclick="btnLoad_Click" />
Code behind events:
protected void rptTest_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Button button = (Button)e.Item.FindControl("btnTest");
button.Text = string.Format("Button {0}", e.Item.DataItem.ToString());
button.CommandName = e.Item.ItemIndex.ToString();
}
protected void rptTest_ItemCommand(object source, RepeaterCommandEventArgs e)
{
Response.Write(string.Format("Postback from button {0}", e.CommandName));
}
protected void btnLoad_Click(object sender, EventArgs e)
{
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);
rptTest.DataSource = list;
rptTest.DataBind();
}
Hopefully i've understood the problem and this helps.
If any of your controls are created dynamically, then they have to be recreated during post back in order for the events etc to get hooked back up.
If this is the case, take a look at a control built by a guy named Denis Bauer. We use this with just some slight modifications and it's perfect.

asp.net Button event inside jQuery FaceBox [duplicate]

This question already has answers here:
Facebox adding commas to input
(3 answers)
Closed 3 years ago.
I'm using jQuery FaceBox to show a textbox, a dropdownlist and a button. The user can write a text in the textbox, select a value in the ddl abd hit the button. This fires some code in the codebehind. The FaceBox shows fine, and the content in it is also ok. Also, the button event is fired. This is the code for the button event handler:
protected void Button1_Click(object sender, EventArgs e)
{
_favorit = new Favoritter();
ListItem fav = ddl_favoritter.SelectedItem;
_favorit.FavoritterFolderID = int.Parse(fav.Value);
//_favorit.FavoritterFolderID = Convert.ToInt32(ddl_favoritter.SelectedItem);
_favorit.FavoritterNavn = txt_favoritNavn.Text;
_favorit.FavoritterUserID = UserID;
_favorit.FavoritterUrl = HttpContext.Current.Request.Url.ToString();
FavoritterManager.InsertFavoritter(_favorit);
}
A business object is created, and its properties set with the values read from the controls. The object is then inserted into a database, which works just fine. The problem is that the textbox and dropdown values are not set properly. The textbox value is empty, and the ddl selected value is allways 1, even though I write in the textbox, and select another ddlitem before I hit the button. The ddl is loaded like this:
if (!Page.IsPostBack)
{
_favoritter = FavoritterFolderManager.GetFavoritterFolderByUser(UserID);
ddl_favoritter.DataSource = _favoritter;
ddl_favoritter.DataBind();
}
I tried putting this code outside if (!Page.IsPostBack), and also filling it using an objectdatasource, still the same issue. It's like the controls are "reset" as I hit the button, and I don't think it has anything to do with the FaceBox, as all it does is to show the div that contains the controls... Then again, it might... Any ideas?
This is the code in the aspx page:
<div id="showme" style="display:none;">
Add to favourites.<br />
<br />
<p>
Title: <span><asp:TextBox ID="txt_favoritNavn" runat="server"></asp:TextBox></span></p>
<p>
select folder: <span><asp:DropDownList ID="ddl_favoritter" runat="server" DataTextField="FavoritterFolderNavn"
DataValueField="FavoritterFolderID" AppendDataBoundItems="true">
</asp:DropDownList>
</span>
</p>
<br />
<asp:Button ID="Button1" runat="server" Text="Gem" onclick="Button1_Click"/>
</div>
You need to have the code that fills the text box and selects the drop down item inside of the if(!IsPostBack) block, because the page load event fires again before the button event (See the ASP.NET Page Life Cycle for more info on this). Have you tried enabling view state on the control? That may be part of the issue.
Change
$('body').append($.facebox.settings.faceboxHtml)
to
$('form').append($.facebox.settings.faceboxHtml)
The problem is a lot of these controls, not just FaceBox append themselves to the body by default. jQuery UI dialog does this as well.
See this question for a fix: JQuery Facebox Plugin : Get it inside the form tag
When things happen outside the <form> tag, they're disconnected from how ASP.Net works. When you clicked submit, the values from those inputs weren't inside the form, so didn't submit to the server...which is why you aren't seeing the values.
This is the quick answer from that question, credit to Kevin Sheffield:
poking around the facebox.js I came across this line in the function init(settings)...
$('body').append($.facebox.settings.faceboxHtml)
I changed that to ...
$('#aspnetForm').append($.facebox.settings.faceboxHtml)

Resources