How to add css to a asp.net web application - c#

I am starting a new asp.net web app class (using c#). For my first lab I need to create a simple web page with a couple button. I have already made the page with the buttons and text. When you press the button it needs to change the text color using in-line css. How would I go about adding the in-line css to the button. Would I add some sort of C# code in the button to enable css? I am quite confused how to add the css to the web app. Any ideas?

From your question it sounds like you want to add inline css to some sort of element containing text when you click a Button?
protected void btn_Click(object sender, EventArgs e)
{
string inlineCss = "your css goes here";
//I'm assuming the text you want to apply css to is a Label with ID=label
label.Attributes.Add("style", inlineCss);
}

Try this on click event
HtmlLink css = new HtmlLink();
css.Href = "css/fancyforms.css";
css.Attributes["rel"] = "stylesheet";
css.Attributes["type"] = "text/css";
css.Attributes["media"] = "all";
Page.Header.Controls.Add(css);
If needs inline
txtmyCtrl.Style.Add(HtmlTextWriterStyle.Color,"#33cc45");

Related

Programmatically create an HTML button (not an <Input> button) using C#

I'm looking to create some HTML buttons via code behind.
I currently have the following code to create INPUT buttons, but I want to be able to have some css hover over events and from what I've learnt this can't be done with INPUT buttons.
This is what I have:
Button btnRemoveFromQuarantine = new Button();
btnRemoveFromQuarantine.ID = "btnDoThis";
btnRemoveFromQuarantine.CssClass = "fa editButton";
btnRemoveFromQuarantine.Click += new EventHandler((s, e) => btnRemoveFromQuarantine_Click(s, e, ID));
This gives me:
Can anyone offer some code to create
Thanks..

How do I return all visible text on a web page as a big, unparsed string?

I am looking for a simple script that would basically do the equivalent of a user pressing Ctrl+A (select all) on a web page and then copying the text to the clipboard so I can pull it into a string from there.
The reason I want to emulate a user selecting all and then copy and pasting is because some pages are generated with Javascript and do not have the visible text in the HTML.
In any case, I am just looking for the raw unparsed text. I do not care if the spacing/line breaks are messed up, etc. I just want a quick and dirty snapshot of all the selectable text on the page into a string.
I have tried doing below as an example:
private void button3_Click(object sender, EventArgs e)
{
HAP.HtmlWeb web = new HAP.HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = web.
Load(#"https://mywebsite");
string str = doc.DocumentNode.InnerText;
MessageBox.Show(str);
}
but if the page has javascript it does not return the text displayed by it.
Instead of
doc.DocumentNode.InnerText;
Use this
doc.DocumentNode.InnerHtml;
It will get you the whole HTML including JS and CSS. Hope it helps.
With jQuery: $(document).text() or $('body').text()

What HyperLink has been clicked in c# asp.net?

I am making an asp.net web application which has to play videos. In my start page I have a hyperlink for each video. All the hyperlinks are identical, except their names. This Means they all link to the same page. Im interrested in knowing wether there is an option to know which hyperlink was clicked. I would like to retrieve the name of the hyperlink.
My code for generating the hyperlinks looks as follows:
foreach (FileInfo i in corFiles)
{
HyperLink t = new HyperLink();
t.Text = i.Name;
t.NavigateUrl = "page.aspx";
CorrectArray.Add(t);
}
return CorrectArray;
The text of the hyperlink is Unique to a video, which Means I can change the src destination of the video to play based on the text name. So the question goes as follows. Are there any way of retrieving the text name of the hyperlink when it is clicked by the user?
I hope you can help! Thanks in advance.
Regards
Magnus
You can add a link buttons instead of hyper links, if you want to make a post back to the same page.
foreach (FileInfo i in corFiles)
{
LinkButton t = new LinkButton();
t.Text = i.Name;
t.Click += new EventHandler(DynamicClick);
t.CommandName = i.Name;
CorrectArray.Add(t);
}
void DynamicCommand(Object sender, CommandEventArgs e)
{
// using e.CommandName and e.CommandArgument you can differentiate the hyperlinks
}
If i understood your question,
you have to add the name attribute in the hyperlink tag and get its values in the code behind by fetching names
You can use JQuery that is integrated into .NET to get the text of the hyperlink upon it is clicked by the user. To be able to access the Hyperlink's text from Javascript simply add it to a custon tag inside the link to have something like this <a href="page.aspx" htext="hyperlink text">. As the hyperlink html is generated from .NET server side code you have to generate this custom tag from the .NET server side context.
Why don't you use a Querystring?
t.NavigateUrl = "page.aspx?video=<someid>"
and in the page you parse the querystring to show the correct video?

Content place holder background image

It is possible to set a background image for content place holder? At the moment , i can display a background image for the whole page but the content place holder is blocking most of the picture so i would like to set a background image for the content place holder instead ( most of the content are in the holder ) .
Here are my code to display the image in background but i got no idea how to place it in content place holder in ASP.NET :
protected void dropListActivity_SelectedIndexChanged(object sender, EventArgs e)
{
if (dropListActivity.SelectedItem.Text == "GrassHopper and Ants")
{
PageBody.Attributes.Add("style", "background:url(Images/background/ant-and-grasshopper.jpg) no-repeat;");
Session["Background"] = "background:url(Images/background/ant-and-grasshopper.jpg);";
}
if (dropListActivity.SelectedItem.Text == "Food Fit For A King")
{
PageBody.Attributes.Add("style", "background:url(Images/background/King.jpg) no-repeat;");
Session["Background"] = "background:url(Images/background/King.jpg);";
}
}
and in my html side , i just add a body id = "PageBody" and it does the job . but how do i do that in content place holder? i am a newbie in programming as well as CSS or Html .
Content place holder only render it's content, there is no html element that is rendered if it is empty, you can put a div inside it and it will render.
And concerning your solution - you are going to the server to change a background image, please don't.
you should do it in JavaScript + CSS
use jQuery to listen to the change event of the select element and then change the background according to it.
in your css:
.grassHopper{background:url(Images/background/ant-and-grasshopper.jpg) no-repeat;}
.foodFit{background:url(Images/background/King.jpg) no-repeat;}
in your JS
$(document).ready(function(){
$("#dropListActivity").change(function(event){
var jqBody = $("body");
jqBody.removeClass("grassHopper foodFit");
if($(this).find("option[selected]").text() === "GrassHopper and Ants")
{
jqBody.addClass("grassHopper");
}
...
}).change();//also trigger so the bg will be updated according to the selected value at the select from the server
});
And last thing - if you are only starting with programming on the .net stack - i suggest useing asp.net MVC and ont webForms

How to add a querystring after url when asp.net button is clicked

I have a asp.net page that has a button to click. When click on it, I wish to have a querystring such as ?id=1 added after the normal url. How can I do that from server side c# code?
Three ways... server-side redirect, LinkButton, and client-side button or link.
You can have your button event handler redirect to a location with a querystring...
Response.Redirect("myPage.aspx?id=" + myId.toString(), true);
You can render the button as a LinkButton and set the URL...
LinkButton myLinkButton = new LinkButton("myPage.aspx?id=" + myId.toString(), true);
Or, you can render the button as a client side link - this is what I do when using Repeater controls...
<a href='myPage.aspx?id=<%# Eval("myID") %>'>Link</a>
I prefer the last method, particularly when I need a whole bunch of links.
BTW, this is application of KISS - all you need is a regular old link, you don't need to jump through server-side hoops to create a link with a querystring in it. Using regular client-side HTML whenever possible is how to keep ASP.Net simple. I don't see enough of that technique in the wild.
I realize this is an old question, but I had the exact same problem when we wanted to create a new URL string so Google Analytics could "count" when a form was submitted.
I was using an ASP:Button to submit the form and saving to a database and displaying a thank-you message on postback. Therefore, the URL pre- and post-submit was the same, and I had to modify it in some way.
I used the following code and it worked for me:
C#, in Page_Load:
btnSubmit.PostBackUrl = "Page.aspx?id=" + id.ToString() + "&newquerystring=newvalue";
where Page.aspx is the page with the button that I want to postback to (the same page), ID is a dynamic ID I'm using to grab content from our CMS, and obviously the new querystring item.
I hope this helps.
There are various way to add querystring in url. You can use following code If you want to add value on server side:
protected void Button_Click(object sender, EventArgs e)
{
Int32 id = 1;
// Or your logic to generate id
string url = String.Format("anypage.aspx?id={0}",id.ToString());
}
How to build a query string for a URL in C#?
Or you can use this code.
string url = Request.Url.GetLeftPart(UriPartial.Path);
url += (Request.QueryString.ToString() == "" ) ? "?pagenum=1" : "?" + Request.QueryString.ToString() + "&pagenum=1";

Categories

Resources