send argument of link to another link asp net C# - c#

I have a default page with a gridview showing different categories.
When I enter a category, the name of the category is passed as an argument in the link, like
http://localhost:49442/category.aspx?category=Windows
I have a link with in there for a "New topic". I want to send the "category" parameter further.
I tried an approach but with no results.
The code is
<a id="topicLink" href="default.aspx">New Topic</a>
And the code in C# is
string x = Request.QueryString["category"];
topicLink.Href = "newtopic.aspx?category=" + x;
Error: the name topicLink does not exist in this context
How can I do this ?

Just add runat="server" in your html code like this:
<a id="topicLink" runat="server" href="default.aspx">
New Topic
</a>
Then you can use topicLink to change the href value just as you did. :)

ok, you want to use something like this when the event fires
$("#bttnId").click(function() {
$("a").attr("href", "your url here")
})
this code is based on a click event, in this instance its a button, but if its coming from a page load event from parameters you might want to take a look at purl.js which is a third party js file that will allow you to grab the parameters from a query string and then you can use this code
$(document).ready(function(){
var url = $.url().param('category');
$("a").attr("href", url)
})
you can get more information about purl from here https://github.com/allmarkedup/purl
please also note this is pseudo code and should be enough to give you a direction of where you want to go

I think you are looking for the HyperLink Control
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="default.aspx">New Topic</asp:HyperLink>
You can then alter the NavigateUrl property in code behind.
HyperLink1.NavigateUrl += "?category=" + x;

I think the best way is to create the hyperlink from the begin with the right URL
<a id="topicLink" href="default.aspx?category="+<%Request.QueryString["category"]%>New Topic</a>

Related

How to get asp:hyperlink to display text instead of url

I have a panel that gets a hyperlink added dynamiclly through C# with values from an sql database.
However some of the URLs are really long and quite uneccessary to display.
I have not found any good ways to hide/disable the url showing and replace it with text. I can not use normal <a href> as it handled server side.
EDIT added some of the code.
<asp:HyperLink ID="moduleHyperlink" runat="server"></asp:HyperLink>
now in C#
HyperLink hyp = createHyperlink(btn.link);
moduleHyperlink.Controls.Add(hyp);
This will display for the user the entire btn.link (url string) which might be really long and it looks messy on the webpage. I would rather have a text saying "External Link", which when clicked redirects the user to the url.
You can add Text property to show some valid link title instead of showing the URL.
HyperLink hyp = createHyperlink(btn.link);
hyp.Text = "YourTextForTheLink";
moduleHyperlink.Controls.Add(hyp);`

URL is not rendering correctly in C#

I have Hyperlink in a datalist. HTML is given below:
<asp:HyperLink ID="lnkEntry" runat="server"><%# DataBinder.Eval(Container.DataItem, "Title") %></asp:HyperLink>
i have saved the URL of a Entry in the Database, like "http://test.com/posts/New-test-in-March" And used the following way to Bind it to Hyperlink in Item_databound of Datalist.
lnkEntry.NavigateUrl = objEntry.PermaLink;
But when it renders into the browser the URL get changed to "http%3a//test.com/posts/New-test-in-March"
i have tried to use the URLDecode but it doesn't make any change in output.
lnkEntry.NavigateUrl = Server.UrlDecode(objEntry.PermaLink);
Please help me how can i fix this issue.
you should use HttpUtility, like this:
lnkEntry.NavigateUrl = HttpUtility.UrlEncode(objEntry.PermaLink);
or WebUtility if running recent versions of .net.
Check this thread for more info.

Find Control in code behind - multiple results controls - nth occurence

I have the following problem.
I have a asp:textbox on the page, runat server with an id of say txt
This text box is in a <div>, nothing special. ie:
<div>
<asp:TextBox id="txt" runat="server"></asp:TextBox>
</div>
The problem is there is some java script which when you push the corresponding button it doubles (copies) the div. This is by design. It is meant to.
When you hit save but at the bottom of the page on a asp:Button, it can't find the value I need because it returns two results.
In the code behind:
(Textbox) blah = (Textbox)senderbutton.FindControl("txt");
string test = blah.text
But the result is essentially--> "The value in the textbox , The value in the textbox"
I.e. it is there twice. I have worked around this by doing the following:
string[] test = blah.text.split(new[] { ',' })
and then only calling the second value in the array or whatever.
BUT, now I have this situation but the problem is that a user can enter a string with a ' , ' in it, hence the splitting goes to crap....
So can I find a control with an id, but only find the nth occurence of it in the code behind?
Seems you need to give different name(like txt0,txt1...) for each copy of the input controls.
You can do this using javascript up on client click(prior to form submission) of your asp button
-- Javascript method
function ModifyName() {
var x = 0;
$("input[name='txt']").each(function () {
$(this).attr("name", $(this).attr("name") + x);
x++;
});
}
-- asp:Button
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
OnClientClick="ModifyName();" onclick="btnSubmit_Click" />
So in code behind you can get the values like this...
protected void btnSubmit_Click(object sender, EventArgs e)
{
var resultArr = Request.Form.AllKeys.Where(x => x.Contains("txt"))
.Select(x => Request.Form[x]).ToArray();
}
Not a very nice solution, but you could examine the Request.Form collection directly upon postback and write some code to process your dynamically added textbox fields.
The best solution would be: avoiding copying the div in js. Since you said "This is by design. It is meant to."(even I really doubt it), there are some alternative solutions:
(1) Don't use the default submit behavior of the form. That is, in the click (js) event of the save button, organize the data in the form and then submit it.
(2) Modify the second(copied) textbox's id so that its id is different from the original one, and then get the data in code behind.
I am not sure why you using FindControl method to find the control when you can directly access the txt control from code behind.
You can get results easily
String test = txt.Text;

Asp Hyperlink and Website link?

I have a hyperlink in an aspx page, whose value is set in the code-behind. The C# code creates website link and sets the NavigateUrl to the URL. The problem is, when the link is clicked, the site address gets appended to existing website address.
e.g.
www.cnn.com <- main site which has hyperlink.
Let's say the new link is 'www.fox.com', when the link is clicked, I get an error, because now the page address looks something like this:
www.cnn.com/www.fox.com
Is there anyway to just display the link which I set behind the code.
ASPX page:
<asp:Hyperlink ID="ltrWebsite" runat="server"/>
C#:
ltrWebsite.NavigateUrl = "www.fox.com";
ltrWebsite.Text = "www.fox.com";
Thanks.
in markup cannot be a literal, it has to be an hiperLink:
<asp:HyperLink ID="ltrWebsite" runat="server"/>
in code behind do not forget the http:// prefix:
ltrWebsite.NavigateUrl = "http://www.fox.com";
ltrWebsite.Text = "www.fox.com";

Dynamically create controls using stringbuilder

i have been trying to create controls dynamically on my web page using the StringBuilder class..and i dont quite seem to get through...
any help would be appreciated.
i am trying to do this...
StringBuilder sbTest = new StringBuilder(string.Empty);
sbTest.Append("<input type=\"text\" id=\"txt1\" runat=\"server\" />");
Response.Write(sbTest.ToString());
The page for sure displays a TextBox on the browser which is easily accessible through JavaScript...but what i want is the control to be available on the Server Side too...so that when the page is posted back to the server i can easliy obtain the value that has been entered by the user into the textbox.
Can any 1 please help me with this....
thank you so much....
Like Torbjörn Hansson says, if you just add a name attribute (and maybe remove runat="server" from your original snippet) you'll be able to access the submitted value but you'll only have a client-side HTML <input /> element.
If you are wanting to dynamically create server-side controls then you'll have to do something like this:
TextBox textbox = new TextBox {
/* take care to create unique ID's if you're adding more than 1 TextBox */
ID = "foo",
Text = "bar"
};
Controls.Add(textbox);
In an answer almost about the something I answered this
You should do the things properly and not trying to reinvent the wheel.
Creating controls Dynamically you can choose 2 ways, the .NET way, or the Javascript way
Both are seen by any of the other, in other words, creating controls using the .NET way, javascript can see and use it and vice versa.
.NET way
in your HTML file add something like
<body>
<form id="form" runat="server">
<asp:PlaceHolder id="ph" runat="server" />
</form>
</body>
in your script part
TextBox txt = new TextBox();
txt.ID = "myTxt";
ph.Controls.Add(txt);
you can easily get that TextBox in javascript using:
var myTxtValue = $("#myText").value();
Javascript Way
var txt = $("<input />", {
id : "myTxt"
});
txt.AppendTo("body");
in .NET you get the value using
string value = Request["myTxt"];
NOTE All javascript lines uses jQuery for simplify results
Provide a name-attribute and access it with:
Request.Form["txt1"]
You can get the value from
Request["txt1"]

Categories

Resources