Get hidden field value in code behind - c#

How can I get the value of the hiddenfield in code behind?
<telerik:RadRotator ID="RadRotator1" RotatorType="AutomaticAdvance" ScrollDirection="Up"
ScrollDuration="4000" runat="server" Width="714"
ItemWidth="695" Height="260px" ItemHeight="70" FrameDuration="1" InitialItemIndex="-1"
CssClass="rotator">
<ItemTemplate>
<div class="itemTemplate" style="background-image: url('IMAGES3/<%# this.GetDayOfWeek(XPath("pubDate").ToString()) %>.png');">
<div class="dateTime">
<div class="time">
<%# (this.GetTimeOnly(XPath("pubDate").ToString())) %>
</div>
<div class="date">
<%# (this.GetDateOnly(XPath("pubDate").ToString()))%>
</div>
</div>
<div class="title">
<span>
<%# System.Web.HttpUtility.HtmlEncode(XPath("title").ToString())%>
</span>
</div>
<div class="buttonDiv">
<asp:Button ID="Button1" class="button" runat="server" Text="View" OnClientClick="OnClick" />
THIS HIDDENFIELD >>>>> <asp:HiddenField id="rssLink" runat="server" value='<%= System.Web.HttpUtility.HtmlEncode(XPath("link").ToString()%>' />
</div>
<div class="description">
<span>
<%# System.Web.HttpUtility.HtmlEncode(XPath("description").ToString())%>
</span>
</div>
</div>
</ItemTemplate>
</telerik:RadRotator>
The hidden field is inside a RadRotator and I am battling to get the value of it in code behind.

You can use it's Value property
var value = this.rssLink.Value;
Edit: for the telerik control it looks like you'll need to use FindControl on the Databind - there's an article here.

when you write something like that in your aspx file you will see that in the designer file you've got a generated property with as name the id of the field you used.
So in the code behind you can use this property because the classes are partial
var value = this.rssLink.Value;
like said before

string hiddenFieldValue = rssLink.Value;

If the Hidden field has the runat="server" attribute then you should be able to access it from the server side using 2 ways:
1- using the value property.
2- using Request.Forms["hiddenFieldName"]
Check this link

Related

How to hide Literal in Codebehind within a ListView

I currently have a panel within a listview, displaying a bunch of literals. There is a column I need to hide in the codebehind of the page. How would I go about this? The panel that contains these literal fields has UpdateMode on always.
<div class="col-md-2 col-sm-3 col-sm-offset-0 col-xs-4 col-xs-offset-0 col-md-offset-0 text-truncate ">
<p class="key">
<asp:Literal ID="Literal4" runat="server" Text='' />
</p>
<p class="text-truncate">
<asp:Literal ID="Literal7" runat="server" Text=' <%# Eval("Status") %>' />
</p>
</div>
If you are aiming to hide them from the view, you can use the code behind as displayed in this answer
Literal4.Visible = false;

Is it possible to add a hyperlink to ASP LinkButton

I have a link button that I want to put a hyperlink to that a popup div is called when the button is clicked.
The asp linkbutton is declared as such:
<TD Class="SpreadSheet"<ASP:LinkButtonID="lbtnViewUsers_Edit" Text="Edit" Runat="Server" onclick="#Upload"/></TD>
and the popup to be called is
<div id="Upload" class="overlay" runat="server">
<div class="popup">
<h2>Edit User &nbsp </h2>
<a class="close" href="#">×</a>
<div class="content">
<!-- content -->
</div>
</div>
</div>
Is there a way to put a sort of href = "#Upload" to the link button?
What you are looking for is a model pop. Asp.net Ajax tool kit has one, you can learn more about it at here . I created a simple sample of how to use this model pop-up. Bear in mind you first have to add the tool kit to your asp.net project (you can use nuget package manager for this) and register the control to page using
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
use the properties of Model pop extender to show and save your per record data.
<asp:Panel ID="pnlpopup" runat="server" Width="400px">
<asp:Panel ID="headerPanel" runat="server">Header text Here </asp:Panel>
<div class="form-group">
<asp:Label ID="Label1" runat="server" Text="lable 1"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
<div class="form-group">
<asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
</div>
</asp:Panel>
<div class="form-group">
Other data values here
<asp:LinkButton ID="lbEdit" runat="server">Edit</asp:LinkButton>
</div>

Call another ASP Page dynamically with different Parameters in ASP.NET C#

I want to call another ASP Page and pass a parameter (entry.ID).
I have a Master-Page Global.master like this (I will only post a part of code):
<body>
<div id="global" style="height:2000px;">
<form runat="server" id="globalForm">
<div id="body">
<asp:ContentPlaceHolder ID="mainContentPlaceHolder" runat="server">
<div id="mainContent">
</div>
</asp:ContentPlaceHolder>
</div>
</form>
</div>
and a Default.aspx Page which uses the Master-Page:
<asp:Content ID="Content1" ContentPlaceHolderID="mainContentPlaceHolder" runat="Server">
<div id="mainContent">
<% ICollection<Database.Blogentry> entries;
entries = Database.BlogentryDBO.Instance.getAllBlogentries();
foreach (Database.Blogentry entry in entries) { %>
<div class="blogEntryBody">
<div class="blogEntryText">
<%= entry.Content %>
</div>
</div>
<div class="blogEntryFooter">
<span class="blogEntryView">
<%--CALL HERE ANOTHER PAGE WITH A BUTTON AND PASS THE PARAMETER entry.ID-->
</span>
</div>
<%
}
%>
</div>
I already tried it with
<label visible="false" id="LabelEntryId" name="LabelEntryId"><%= entry.ID %></label>
<asp:button ID="Button2" runat="server" class="buttonBlogEntry" text="view more ..." onclick="viewEntireBlog_onclick" CommandArgument='<%# Eval("LabelEntryId")%>' />
but I don't have a CommandEventArgs element in the onClick method but only a EventArgs element so I can't access my CommandArgument?!
And using
<input runat="server" type="text" id="LabelEntryId" value="<%= entry.ID %>" visible="false"/>
and then accessing the input-element in the code-behind isn't possible either, because i have more than one LabelEntryId-Input because of the loop.
Another idea was to do something like this
<asp:button ID="Button1" runat="server" class="buttonBlogEntry" text="view more ..." onclick="<%= Response.Redirect("BlogEntrySortedByCategory.aspx?entryID=" + entry.ID); %>"/>
but I don't know how to do that...
Can you help me please?
have you tried with a standard a tag?
<a href='/someotherpage.aspx?entryid=<%= entry.Id %>'>View more</a>

Eval() and expression code

I have such code in ASP.NET web page but when I run it I get error.
> Databinding methods such as Eval(), XPath(), and Bind() can only
> be used in the context of a databound control.
Where is the problem?
<% if (Helpers.GetInt(Eval("Price")) != 0)
{ %>
<input type="button" name="btnSignUp" value="Sign Up - Plimus »" onclick="window.location='<%#Eval("BuyUrl2")%><%= Common.GetUserIdUrl("&","custom_user_id") %>'" />
<% } %>
I get error code on line
<% if (Helpers.GetInt(Eval("Price")) != 0)
I complete Repeater code is below. As I said early everything in this code works fine except IF statement. I want to evaluate PRICE and if it is !=0 show button html code.
<ItemTemplate>
<div class="row">
<div class="col title">
<%#Eval("Title")%>
</div>
<hr/>
<div class="col price">
<%#string.Format("{0} USD/{1} </br> Instant activation", Helpers.GetDecimal(Eval("Price")).ToString("N"), Portal.GetMembershipTypeLabel(Helpers.GetInt(Eval("Credits"))))%>
</div>
<div class="col">
<input type="button" name="btnSignUp" value="Sign Up - PayPal »" onclick="window.location='<%#Eval("BuyUrl1")%><%= Common.GetUserIdUrl("&","custom_user_id") %>'" />
<br />
<br />
<% if (Helpers.GetInt(Eval("Price")) != 0)
{ %>
<input type="button" name="btnSignUp" value="Sign Up - Plimus »" onclick="window.location='<%= Eval("BuyUrl2")%><%= Common.GetUserIdUrl("&","custom_user_id") %>'" />
<% } %>
</div>
<hr/>
</div>
</ItemTemplate>
yes, you are trying to use Eval in an HTML input control, which is not allowed as stated correctly by the error.
Eval or Bind and fellows are executed usually when you bind a data-boundable control like DataList, DataGrid, DataRepeater and so on because these commands (Eval...) are applied against the DataSource you are binding. doing so against a control like raw HTML input has no meaning because there is nothing to bind against.
notice that your call/usage of <%= Common.GetUserIdUrl... looks correct and can stay there :)

Having problem in puting JavaScript Menu into Asp.Net page

Before I post my problem here, please be noted that i would really like to leave javascript and css for that menu as it is.
Now to my problem.
I have a menu in JavaScript that I am having a problem putting into asp.net page, I am having problem to produce the proper html to be more correct.
I would really appreciate if someone could point me to the right direction.
the menu in html looks like:
<!-- HOME -->
<div class="menu_item" onmouseover="hide_all_panels();">
Home
</div>
<!-- ABOUT SITE -->
<div id="trigger1" onmouseover="show_panel('0');">
<div class="menu_item">
About Us
</div>
<div class="hidden_div">
<!-- ABOUT WEB SITE POPOUT -->
<div class="menu" id="popout1">
<div class="menu_item">
Frequently Asked Questions
</div>
<div class="menu_item">
Our Team
</div>
<div class="menu_item">
The Board
</div>
</div>
</div>
</div>
Blog
</div>
<!-- CONTACT US -->
<div class="menu_item" onmouseover="hide_all_panels();">
Contact Us
</div>
as you can see, the divs are not symmetric for single menu that has not children I have simply link wraped into div, but for the menu with childrens I have a menu with one trigger div and 2 more somewhat main divs inside it.
I tryed to put something like that for this menu on asp.net side (don't put atentions to link namings now, they are not important):
<asp:Repeater ID="rpt_Menu" runat="server" OnItemDataBound="rpt_Menu_DataBound">
<ItemTemplate>
<asp:Panel ID="pnl_MainSubmenuDiv" runat="server" Visible="false" Enabled="false">
<div id="trigger<%= index %>" onmouseover="show_panel('<%= index %>');">
</asp:Panel>
<div class="menu_item" onmouseover="hide_all_panels();">
<%# Eval("menu_name") %>
</div>
<asp:HiddenField ID="hdn_Id" runat="server" Value='<%# Eval("menu_id") %>' />
<asp:Repeater ID="rpt_SubMenu" runat="server">
<ItemTemplate>
<div class="menu" id="popout<%= index %>">
<div class="menu_item">
<%# Eval("menu_name") %>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:Panel ID="pnl_MainSubmenuClose" runat="server" Visible="false" Enabled="false">
</div>
</asp:Panel>
</ItemTemplate>
</asp:Repeater>
and the code behind is very simple and there is nothing special, all i do is just bind second repeater that inside first repeater and make panels visible or invisible:
protected void rpt_Menu_DataBound(object obj, RepeaterItemEventArgs e)
{
int parent_id = Int32.Parse(((HiddenField)e.Item.FindControl("hdn_Id")).Value.ToString());
using (SamaraDataContext mycity = new SamaraDataContext())
{
var subMenu = from sm in mycity.tbl_menus
where (sm.menu_parent == parent_id)
select new
{
menu_id = sm.menu_id,
menu_name = sm.menu_name
};
int count = 0;
foreach (var item in subMenu)
{
count++;
}
if (count > 0)
{
((Panel)e.Item.FindControl("pnl_MainSubmenuDiv")).Visible = true;
((Panel)e.Item.FindControl("pnl_MainSubmenuClose")).Visible = true;
((Repeater)e.Item.FindControl("rpt_SubMenu")).DataSource = subMenu.ToList();
((Repeater)e.Item.FindControl("rpt_SubMenu")).DataBind();
this.index++;
}
}
}
}
However my problems is that panels produce div's even if they are hidden, which breaks out all the html structure.
I would really dislike to put the divs formating inside the code behind.
Well for one thing this section here:
<asp:Panel ID="pnl_MainSubmenuClose" runat="server" Visible="false" Enabled="false">
</div>
</asp:Panel>
Will render invalid HTML if Visible is set to true, thus potentially screwing up your JavaScript and CSS interactions.
That will come out like so:
<div id="something_something_pnl_MainSubmenuClose">
</div>
</div>
as Panel controls render a around whatever they enclose. So figure a way around that, and you'll likely solve your problem.
To supplement blesh's answer, use the PlaceHolder control instead of Panel. Placeholders do not render HTML surrounding the contents.

Categories

Resources