Dynamically add Css asp.net page [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have 3 css files (File.css,File2.css,File3.css) with same class name "BkImg" which changes the background image of the page and depending upon some condition in my codebehind page I want to link one of these file.
In my aspx body tag ().
I am using C# as code behind language

Similar to this question.
An adaptation of an answer on that question..
You can use the Page_Init function in your code behind file to dynamically generate the link and add it to your page header (or body, in your case). An example of that function is below in C#. You would, of course, implement your logic to change the Href value.
protected void Page_Init(object sender, EventArgs e)
{
var link = new HtmlLink();
link.Href = "~/styles/main.css";
link.Attributes.Add("rel", "stylesheet");
link.Attributes.Add("type", "text/css");
Page.Body.Controls.Add(link);
}
Make sure that you put a runat="server" in the body tag so that you can reference the body from the code behind file.
<body runat="server">
</body>

First add 'id' and 'runat' property for body tag.
<body id="mybody" runat="server">
then you can add your calss dynamically with page_load event or another event.
protected void Page_Load(object sender, EventArgs e)
{
mybody.Attributes.Add("class", "classname();");
}

Related

How to Commit changes of HTML code permanently ? C# ASP.NET [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
First I create a page with a textbox and a button; when the button is clicked, it redirects to another page.
This is the code for the redirected page:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.PreviousPage != null)
{
TextBox SourceTextBox =
(TextBox)Page.PreviousPage.FindControl("TextBox");
if (SourceTextBox != null)
{
form1.InnerHtml = SourceTextBox.Text;
}
}
}
Now, I've what is written in the text box is displayed but the changes in the form aren't permanent. When I close the page then I open it again, it doesn't display what I have written before.
Is there any way to make the changes in the form permanent when I use .innerHtml?
Not without adding some storage solution. All you're doing here is editing the inner HTML of the form in the single instance of the page being rendered on the server, any other users loading the page will not see your changes, and as you have already discovered reloading the page yourself discards the content and reloads from the server.
You could save the content to a file on the server, store it in a database on your server, or perhaps use an online solution like Google's Firebase if you don't have access your your own DB server.

Alternative for asp:panel in asp.net mvc [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Now that every thing is being converted from the conventional aspx to razor view because of its benefits,
my question is how to achieve thing similar to panel, where in we were allowed that if X is true, show the panel else visible = false.
How can we achieve a similar thing in MVC?
You can do it with razor in the view:
#if(condition) {
<div>............ </div>
}
The panel is actually <div id="yourdivid">...</div> element in MVC view, thus you can either process condition directly on view as Matteo1010 said, or use JS if you want to setup views with only passed values and HTML helpers:
<script type="text/javascript">
var condition = '#[passed condition value here]';
if (condition) {
// show panel
document.getElementById("yourdivid").style.visibility = "visible";
}
else {
// hide panel
document.getElementById("yourdivid").style.visibility = "hidden";
}
</script>
<div id="yourdivid"></div>
I think JS approach with passed value has more advantage to control the view behaviour in client-side than write C# code directly inside the view.

Anyway to avoid buttons when it comes to page load? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have created a program that runs through a flow using C#
At the moment it contains a large amount of buttons to make certain panels visible etc.
I was wondering if I could make the page refresh when the user checks a checkbox?
You need to set the CheckBox' AutoPostBack-property to true(default is false).
<asp:CheckBox id="checkbox1" runat="server"
AutoPostBack="True"
Text="Check/uncheck me for a postback"
OnCheckedChanged="Check_Clicked"/>
IF you want to refresh your panel on check of checkbox, you can do something like below:-
<asp:CheckBox id"chkVisible" runat="server" AutoPostBack="true" OnCheckedChanged="chkVisible_OnCheckedChanged" Text="Test for Page referesh" />
You need to set AutoPostBack property to True for refreshing your page.
Try this :
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//do whatever you want to on first time page load
}
}

Call C# function from asp.net [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Good morning,
I have the following asp Image and would like to add the on mouse over event, as follow:
<asp:Image ID="MapImage" runat="server" Height="601px" Width="469px" OnMouseOver="OnMouseOverMap"/>
I have also added the following method on the C# code:
protected void OnMouseOverMap(object sender, EventArgs e)
{
int i = 9;
}
I also have created the same method without the parameters but I cannot manage to call that C# function.
Can somebody help me with this issue?, How can I do to call the C# function from the ASP code.
Cheers!
There is no OnMouseOver server-side event on asp:Image object.
What you can do you can write js function called on client-side onmouseover event and inside that function trigger click on hidden button or you can change Image to ImageButton and trigger click on that image.
<asp:Image ID="MapImage" runat="server" Height="601px" Width="469px" onmouseover="javascript:foo()"/>
<asp:Button ID="Button1" runat="server" style="display:none" OnClick="OnMouseOverMap" />
And in js function:
function foo()
{
document.getElementById('<%= Button1.ClientID %>').click();
}
I am pretty sure that you are supposed to insert javascript code there and not a "code behind event handler reference". See ASP NET image onmouseover not working for example.

How to save pages with unique names? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i am using C# and sql database. In db is stored information about items - id, name, etc.
Now i have a page to show items, that i use as a template and pass Item id in the url like /Item.aspx?id=223.
I would like to create pages for every item and save them to folder like /Items/Red-Book.aspx.
Any suggestions?
Thanks, Walt
You don't need to save the pages physically. You only need one page Item.aspx which will generate the content according to the ItemiId you supply. (the same way you explained in the question)
After you have this page you need to look into how to rewrite URL.
What rewrite URL does is that it takes the url that a user requested and rewrites it to something else.
If a user request /Items/Red-Book.aspx the url will be rewrited to /Item.aspx?id=223 and youre ASP.NET will load /Item.aspx?id=223 back to the client.
You can make use of the method RewritePath which you can call at Application_BeginRequest to rewrite your URL.The logic would be as follow:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
var currentPath = HttpContext.Current.Request.Path;
// do some logic to generate newPath
var newPath = GetNewPath ( currentPath );
HttpContext.Current.RewritePath(newpath);
// after this point ASP.NET will work as the user would have requested newpath
}

Categories