Adding a glyphicon to a control through c# - c#

Dim glyph As HtmlControls.HtmlGenericControl
glyph = New HtmlControls.HtmlGenericControl
glyph.InnerHtml = "<span class=""glyphicons glyphicons-restart""></span> Reset"
btnBarButtonBar.SummaryButton.Controls.Add(glyph)
I currently am doing it through VB like this but could anybody convert this for me to C# please? I just need to replace the current summary button which is built through the framework and directs the user to another page and replace it with this button which resets all values to blank. I have the .asp pages working already using the above code, I now need to convert to so it works in aspx/c#

var glyph = new HtmlControls.HtmlGenericControl();
glyph.InnerHtml = "<span class=\"glyphicons glyphicons-restart\"></span> Reset";
btnBarButtonBar.SummaryButton.Controls.Add(glyph);

Related

Open a URL and pre-fill a textbox in an ASPX page from code-behind

So what I'm doing is a little different than this:
Access a control's property in an aspx page from another aspx page
because what I'm trying to do is "push" a value forward to the next page rather than "pull" it from the previous page.
What I need to do is open a new page from within an Intranet website application (actually, the page will have been previously used by the user) and pre-fill a textbox.
Can this be done? I'm currently forwarding to the new page using:
String SUrl = "frmAuditSearch.aspx";
Server.Transfer(SUrl, true);
What I also need to do is fill a textbox called txtAuditID on frmAuditSearch.aspx with the AuditID from the current page I'm on.
And yes, I know I can use something like:
String SUrl = "frmAuditSearch.aspx?AuditID = '" + MyAuditID + "'";
Server.Transfer(SUrl, true);
but I'm trying to avoid that because most of the time there's no AuditID and that'd require tinkering with too much code.
Sounds like you'd want to fill the text box with something like this.
txtAuditID.Text = (Request["AuditID"] ?? String.Empty).ToString().
This allows the absence of the parameter if needed

How to avoid C# adding an escape character when obtaining string value from asp.net resource file

I am trying to pass a Unicode character corresponding to fontawesome icons to the text property of an asp.net web forms button control from the code behind. It works fine when I do the following:
button1.Text = "\xf044";
However I have to get it from a resource file to conform with the company requirement. When I set the resx file with name/value as "Edit" and "\xf044". The problem is that when I set the Text property equal to this value from resx, it is set automatically as follows.
button1.Text = "\\xf044";
Is there anyway to avoid this? I tried something as follows
button1.Text = myResx.Edit.Substring(1);
Now the button instead of displaying the corresponding font icon, simply displays "\xf044".
Thanks

Add html text to webcontrol Button in ASP codebehind file

So I am looking for a way to add HTML text to a programmatically created button, or some other control that would accept HTML text and have a on_click_event to capture.
I have a table that I am building dynamically in the codebehind file based upon steps (attendance to a lesson, and the taking of a quiz) that are recorded as complete or incomplete in the database. If a step was not done, I entered a button with a simple X
If the step was done, I just added HTML month and day to the cell.
Attendance to the lesson is a step that can be repeated, so I wanted to make the lesson button look like the styling of a completed quiz, but still capture a 2nd+ attendance point with a click_event.
I am stumped in making a dynamically created button work with my HTML styling. My text style on an ASP.net webcontrol button in a codebehind file is being interpreted as literal text on that button.
Here is the code that fills the cells, either creates the buttons, or adds the text
if(lesson_completed == true)
{
DateTime date = Convert.ToDateTime(lesson);
Button markNewDateComplete = new Button();//System.Web.UI.WebControls.Button Class
markNewDateComplete.Text = "<font style='font-size:50%;color:silver;'>" + date.ToString("MMMM") + "</font><br><font style='color:light gray'>" + date.ToString("dd") + "<font>";
markNewDateComplete.CssClass = "mybtn";
markNewDateComplete.CommandName = lesson_detail_id.ToString();
markNewDateComplete.Click += new EventHandler(this.NewDateLesson_Click);
tc2x.Controls.Add(markNewDateComplete);
}
else
{
DateTime date = Convert.ToDateTime(quiz);
tc2x.Text = "<font style='font-size:50%;color:silver;'>" + date.ToString("MMMM") + "</font><br><font style='color:light gray'>" + date.ToString("dd") + "<font>";
}
Here is what the output looks like on-screen
There are many pages in SO that give insight how to do this in the regular ASPX page, but none that I could find to do this programmatically in a ASPX.cs codebehind file.
In programming there is always another way to do something; so I am looking for a way to add HTML text to a programmatically created button, or some other small control that would accept HTML text and have a on_click_event that I could use as a method back to SQL.
Example of what I want, but this is not for the codebehind file.Font awesome inside asp button

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?

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