How to Pass Text Box Value to Query String Using HyperLink Field In Client Side Using Asp.Net - html

Iam developing application in asp.net c#, in that i have a requirement to pass text box value through query string without using server side code,
in that iam trying some code below, but its not working for me.
<asp:HyperLink ID="hlnkHistory" runat="server" Text="History" ForeColor="Green" ImageUrl="~/images/History2.png" Font-Bold="true" Target="_blank" NavigateUrl="~/WebFormReports/History.aspx?SerialNo=<%#Eval('txtSerialNo.ClientID')%>" ToolTip="View History"></asp:HyperLink>
When iam assign value directly like the below code its working fine.
<asp:HyperLink ID="hlnkHistory" runat="server" Text="History" ForeColor="Green" ImageUrl="~/images/History2.png" Font-Bold="true" Target="_blank" NavigateUrl="~/WebFormReports/History.aspx?SerialNo=1" ToolTip="View History"></asp:HyperLink>
Note: i need not server side code to pass value
like
protected void lnkNavigate_Click(object sender, EventsArgs e)
{
Response.Redirect("MyLocation.aspx?value=" + myTextBox.Text, false);
}
if it is not possible means please tell me is there any another option to pass text box value to query string like a href & link button
any help would be appreciated.. thanks in advance..

If you want to still use the asp:HyperLink then you can use this snippet.
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:HyperLink ID="hlnkHistory" runat="server" Text="History" ForeColor="Green" ImageUrl="~/images/History2.png" Font-Bold="true" Target="_blank" NavigateUrl="~/WebFormReports/History.aspx?SerialNo=" ToolTip="View History" onclick="setHyperlink()"></asp:HyperLink>
<script type="text/javascript">
function setHyperlink() {
var value_hyperlink = $("#<%=hlnkHistory.ClientID %>").attr("href");
var value_textbox = $("#<%=TextBox1.ClientID %>").val();
$("#<%=hlnkHistory.ClientID %>").attr("href", value_hyperlink + value_textbox);
}
</script>
If you don't need that hyperlink, you can use a somewhat simpler javascript function.
location.href = "~/WebFormReports/History.aspx?SerialNo=" + $("#<%=TextBox1.ClientID %>").val();

Related

adding variables to Server tag [duplicate]

This works:
<span value="<%= this.Text %>" />
This doesn't work:
<asp:Label Text="<%= this.Text %>" runat="server" />
Why is that?
How can I make the second case work properly, i.e., set the label's text to the value of the "Text" variable?
Use Data binding expressions
<asp:Label ID="Label1" runat="server" Text="<%# DateTime.Now %>" ></asp:Label>
Code behind,
protected void Page_Load(object sender, EventArgs e){
DataBind();
}
you can do this
<asp:Label ID="Label1" runat="server" ><%= variable%></asp:Label>
You will need to set the value of the server control in code
First of all, assign an ID to the label control so you can access the control
<asp:Label ID="myLabel" runat="server" />
Then, in your Page_Load function, set the value of your labels 'Text' field
protected void Page_Load(object sender, EventArgs e)
{
myLabel.Text = 'Whatever you want the label to display';
}
This function will be in your code behind file, or, if you are not using the code behind model, inside your aspx page you will need
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
myLabel.Text = 'Whatever you want the label to display';
}
</script>
Good luck.
In my code i am using something like this easily but in the databound control like ListView Item template
<asp:HyperLink ID="EditAction" class="actionLinks" Visible='<%#Eval("IsTrue").ToString() != "True"%>' runat="server" NavigateUrl='<%# Eval("ContentId","/articles/edit.aspx?articleid={0}")%>' />
But when i tried to use outside the databound control using <%# .. %>, it simply doesn't work.
You can easily do with
My href
But for server controls, and outside of databound control. We need to call DataBind() in pageload event explicitly
<asp:Hyperlink ID="aa" NavigateUrl='<%#myHref%>' >
Not sure how to mark this as such, but this is a bit of a duplicate. See this thread.
I don't think embedding code in to your markup will really make your markup any clearer or more elegant.
<asp:Label> is compiling at runtime and converting to html tags. You can set text with codebehind or like this:
<asp:Label id="Text1" runat="server" />
<% Text1.Text = this.Text;%>
UPD: Seems like my variant doesnt work, this is better:
protected void Page_Load(object sender,EventArgs e)
{
Text1.Text = this.Text;
}
Just pitching this little nugget in for those who want a good technical breakdown of the issue -- https://blogs.msdn.microsoft.com/dancre/2007/02/13/the-difference-between-and-in-asp-net/
I think the crux is in pretty decent agreement with the other answers:
The <%= expressions are evaluated at render time
The <%# expressions are evaluated at DataBind() time and are not evaluated at all if DataBind() is not called.
<%# expressions can be used as properties in server-side controls. <%= expressions cannot.

How to navigate to a section on postback?

I have multiple sections on my aspx page.
<ul>
<li class="tab-current"><asp:HyperLink ID="home" runat="server" Text="Home" NavigateUrl="#section-1"/></li>
<li><asp:HyperLink ID="events" runat="server" Text="Events" NavigateUrl="#section-2"/></li>
<li><asp:HyperLink ID="finance" runat="server" Text="Finance" NavigateUrl="#section-3"/></li>
<li><asp:HyperLink ID="merchandise" runat="server" Text="Merchandise" NavigateUrl="#section-4"/></li>
<li><asp:HyperLink ID="tasks" runat="server" Text="Tasks" NavigateUrl="#section-5"/></li>
<li><asp:HyperLink ID="profile" runat="server" Text="Profile" NavigateUrl="#section-6"/></li>
</ul>
On one of the sections I have a button and a gridview control. On button click gridview gets loaded with some data, However button click causes postback and takes me Section#1. I want to stay on the that gridview after postback.
Thanks.
Add MaintainScrollPositionOnPostback="true" to the Page directive.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="myPage.aspx.cs" MaintainScrollPositionOnPostback="true" %>
Or if you want this for all your pages you could set in the Web.Config under the <system.web> node.
<pages validateRequest="true" maintainScrollPositionOnPostBack="true" enableViewStateMac="true" enableEventValidation="true" viewStateEncryptionMode="Always"/>
UPDATE
To make sure the correct tab is displayed after PostBack, you need to call a client side function.
First add something like this to the section onclick="setTabIndex(1)". This can also be onmouseover, a binding on class name etc.
<asp:HiddenField ID="HiddenField1" runat="server" />
<script type="text/javascript">
function setTabIndex(tab) {
//store the tab number in a hidden field so you can access it in code behind
document.getElementById("<% = HiddenField1.ClientID %>").value = tab;
}
function showTab(tab) {
alert(tab);
//open the correct tab
}
</script>
And in code behind call the client side function showTab:
if (IsPostBack)
{
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "showTab", "showTab(" + HiddenField1.Value + ")", true);
}
One thing you could do is to pass back the url of the current page with the section ID anchor appended to the URL (see below).
Example:
http://yoursite.com/return-address#section1
http://yoursite.com/return-address#section2
and so on...
Hope this helps.
Craig

How to access Input type="Checkbox" control inside <dx:ASPxGridView/>(Dev Express) at server side(c#)

I have a dx:ASPxGridView (Dev express gridview) which contains HTML checkboxes - input type="checkbox" control.
Problem is how can i access these HTML checkboxes at server side, so that i can check those checkboxes according to condition.
Please suggest me how can i access these HTML checkboxes on server side.
Code image can be found here
Thanks
Girish Rawat
First, to access a HTML element on a server, you need to mark it with the runat="server" attribute.
To access your checkbox, handle the ASPxGridView.HtmlDataCellPrepared event. In the event handler, you can get your input by using the ASPxGridView.FindRowCellTemplateControl method. You will also need to set ID for the input.
<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" OnDataBinding="ASPxGridView1_DataBinding" OnHtmlDataCellPrepared="ASPxGridView1_HtmlDataCellPrepared" KeyFieldName="ID">
<Columns>
<dx:GridViewDataCheckColumn VisibleIndex="0">
<DataItemTemplate>
<input id="check" type="checkbox" runat="server" />
</DataItemTemplate>
</dx:GridViewDataCheckColumn>
</Columns>
<Styles AdaptiveDetailButtonWidth="22"></Styles>
</dx:ASPxGridView>
protected void ASPxGridView1_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e) {
HtmlInputCheckBox check = (HtmlInputCheckBox)ASPxGridView1.FindRowCellTemplateControl(e.VisibleIndex, e.DataColumn, "check");
check.Checked = GetValue((int)e.KeyValue);
}
public bool GetValue(int id) {
// your logic
return id % 2 == 0;
}

Issue related to inbuilt javascript function of Telerik controls

I have asp.net webapplication having some telerik controls.
i have a RadTextBox(txtSearch) and RadButton(btnSearch) on .aspx page.
i have written following validation for empty Textbox:
$('#btnSearch').click(function () {
if ($('#txtSearch_text').val() == '') {
$('#txtSearch_text').addClass('validation');
return false;
}
else {
$('#txtSearch_text').removeClass('validation');
}
});
in validation class i have set Border-left:2px solid red
now problem is that when i click on btnSearch it sets validation class to txtSearch textbox, but when i use mouseover on txtSearch textbox class name suddenly changed to someother from inbuilt javascript function of Telerik. in this Javascript function of telerik TextBox, it changes class name of textbox to another class.
and this execution of change class occurs after executing custom javascript function.
so i want to execute customer javascript function after executing inbuilt functions of telerik. how to do it?
Thanks
You can define invalid states for RadInputs. RadTextbox by itself cannot be invalid because you can put anything in a textbox (unlike a numeric textbox, for example), yet here is a starting point:
<telerik:RadTextBox ID="RadTextBox1" runat="server">
<InvalidStyle BackColor="Red" />
</telerik:RadTextBox>
<asp:RequiredFieldValidator ID="TextBoxRequiredFieldValidator" runat="server" Display="Dynamic"
ControlToValidate="RadTextBox1" ErrorMessage="The textbox can not be empty!">
</asp:RequiredFieldValidator>
<telerik:RadButton ID="RadButton1" runat="server" OnClientClicked="test" Text="submit"
AutoPostBack="false" />
<script type="text/javascript">
function test() {
var tb = $find('RadTextBox1');
if (tb.get_value() == "") {
$find('RadTextBox1')._invalidate();
$find('RadTextBox1').updateCssClass();
}
}
</script>
Tampering directly with the HTML of complex controls may get you nowhere because they will try to update/fix their state according to the logic they have.

How To Highlight Dynamically Loaded Menu Item In C#?

I've been trying to find an easy way of highlighting the current selected menu item of an asp.net menu (so the user knows which page they are on), but no matter what I have tried I can't get it to work. In my markup I have:
<asp:Menu SkinID="modulesMenu" DataSourceID="modulesSource" runat="server" ID="ModulesMenu"
OnMenuItemDataBound="ModulesMenu_MenuItemDataBound">
<StaticItemTemplate>
<div>
<asp:HyperLink ID="HyperLink2" CssClass="moduleName"
NavigateUrl='<%# ((MenuItem)Container.DataItem).NavigateUrl %>'
Text='<%# ((MenuItem)Container.DataItem).Text %>' runat="server" />
</div>
</StaticItemTemplate>
<DynamicSelectedStyle ForeColor="Red" Font-Bold="true" />
</asp:Menu>
This is the c#code used to identify the selected item
protected void ModulesMenu_MenuItemDataBound(object sender, MenuEventArgs e)
{
SiteMapNode siteMapNode = (SiteMapNode)e.Item.DataItem;
string pathStringFormat = IsNodeActive(siteMapNode) ? ConfigurationManager.AppSettings["ModuleImagePathFormatString_Active"] : ConfigurationManager.AppSettings["ModuleImagePathFormatString_Inactive"];
e.Item.ImageUrl = String.Format(pathStringFormat, siteMapNode["imageName"]);
if (IsNodeActive(siteMapNode))
{
e.Item.Selected = true;
}
}
private bool IsNodeActive(SiteMapNode siteMapNode)
{
if (SiteMap.CurrentNode != null)
{
return (SiteMap.CurrentNode.Equals(siteMapNode) || SiteMap.CurrentNode.IsDescendantOf(siteMapNode));
}
return false;
}
I can give you an idea. In ModulesMenu_MenuItemDataBound compare the item url and physical file of the request by the following code
Path.GetFileName(Request.PhysicalPath).ToUpper()
You can use can compare both and set selected css-class using compare as follows
lstrMenuUrl.ToUpper().Replace(" ", "").Contains(filename.ToUpper())
where lstrMenuUrl is the current menu which is clicked.
Here is a link which may help you
Highlight Selected Tab in ASP.Net Menu
I wrongly set the attribute .The attribute to set was
<Staticselectedstyle ForeColor="Red" Font-Bold="true" />
instead of
<DynamicSelectedStyle ForeColor="Red" Font-Bold="true" />