I am trying to get the actual HTML text for the control from the client browser because some of the info in this control don't show on the sever side special the "Style". So How can get the whole HTML text for a control?
<asp:TextBox ID="tb1" runat="server" OnChange="tbOnChange();" />
<script type="text/javascript">
function tbOnChange() {
var tb1 = document.getElementById('tb1');
alert(tb1.value);
}
</script>
EDIT: For get style in C#
ASP page:
<input type="text" id="tb1" runat="server" style="color:Red;" />
C# page:
protected void Page_Load(object sender, EventArgs e)
{
var style = tb1.Style;
}
Regards
k.
Related
I have to scrap data from website, i can do this using Import.io but i have to learn that how can i do by write simple program in C#.
Assume that page contain One textbox and one button , and when click on button its give search result .
Page1 : www.example.com
Search : <input type="text" value="abc" id="search" >
<input type="submit" id="submit">
Page2: www.example.com/result.aspx
<body>
<p>you have entered abc</p>
</body>
Now i want only data of result.aspx when i run my localhost:5585/deafult.aspx.
Thanks in advance
You need to pass the data which has been input in page 1 to page 2 somehow. The simplest way would be to use a query string parameter.
Update your Page 1 mark up to use server controls:
<asp:TextBox ID="search" runat="server" />
<asp:Button ID="submit" runat="server" OnClick="btnSubmit_OnClick" />
Create a click event for the submit button:
protected void btnSubmit_OnClick(Object sender, EventArgs e)
{
var redirectUrl = string.Format("/result.aspx?input={0}", search.Text);
Response.Redirect(redirectUrl);
}
Then in Page 2, on page load you want to grab this from the query string:
protected void Page_Load(Object sender, EventArgs e)
{
var input = Request.QueryString["input"];
if (!string.IsNullOrEmpty(input))
{
litResult.Text = input;
}
}
You'll need to update your Page 2 mark up slightly also:
<body>
<p>you have entered <asp:Literal ID="litResult" runat="server" /></p>
</body>
I am trying to create Div dynamically on the press of button click.
For that i refered this link>> http://forums.asp.net/t/1349244.aspx
and made code on server side(.cs page) as follows>>
public static int i = 0;
protected void Button1_Click(object sender, EventArgs e)
{
i++;
HtmlGenericControl newControl = new HtmlGenericControl("div");
newControl.ID = "NEWControl"+i;
newControl.InnerHtml = "This is a dynamically created HTML server control.";
PlaceHolder1.Controls.Add(newControl);
}
This code was giving me just one div each time when i press the button., I wanted to have addition of divs.
On client side using javascript also i tried>>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" OnClientClick="addDiv();" />
</div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</form>
</body>
</html>
<script type="text/javascript">
function addDiv() {
alert("Control comming in function");
var r = document.createElement('Div');
r.style.height = "20px";
r.style.width = "25px";
r.appendChild("div");
alert("Control going out of function");
}
</script>
Both of these didnt work.
What mistake am i making?
Is there any thing wrong?
Use this
public int Index
{
get
{
if(ViewState["Index"]==null)
{
ViewState["Index"]=0;
}
else
{
ViewState["Index"]=int.Parse(ViewState["Index"].ToString())+1;
}
return int.Parse(ViewState["Index"].ToString());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
HtmlGenericControl newControl = new HtmlGenericControl("div");
newControl.ID = "NEWControl"+Index;
newControl.InnerHtml = "This is a dynamically created HTML server control.";
PlaceHolder1.Controls.Add(newControl);
}
It is giving you one div, cause you are adding one div.
Remember that asp.net needs you to create all dynamically added controls on very PostBack after that.
If you want two controls you have to add two to the PlaceHolder.
Just use one parent div with some ID(predefined lets say id="dvDynamic") and runat="server"
and then use it the dvDynamic.innerHTML = "<div> /* dynamic div contents */ </div>"
Its the simplest way, as if you are using html element in ASP.net use dom controls for better generation. Dynamic creation of control will require handled, interface and many things to co-ordinate with that control. As its not predefined by system. you have to create it.
SO choose the DOM element option. that is faster and better :)
I hope this will help :)
I used the code below to make a change in div of the page of this code file:
HtmlGenericControl divTest = Page.FindControl("test") as HtmlGenericControl;
divTeste.InnerText = "tested";
But now, I want to change another page. Ex.: default.aspx code behind changes a div inner text in the test.aspx ...
The code above isn't appropriated. I want permanent changes. What code should be?
How can I do it?
You need to understand how ASP.NET works. Introduction to ASP.NET
If you are on Default.aspx and then wont to move to Test.aspx you can use values from Default.aspx in Test.aspx however you can't just change different pages from the page you are on. (Actually it is possible by writing data to a database and then later accessing that data on the page you want to change but for your example I don't believe you are looking for something like that)
The closest to your example I can think of is using a Query string.
Default.aspx Markup
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
Default.aspx Code behind
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Test.aspx?DivMSG=Hello");
}
Test.aspx Markup
<div id="MyTestDiv" runat="server">
</div>
Test.aspx Code Behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MyTestDiv.InnerText = Request.QueryString["DivMSG"];
}
}
Update:
One way to make it permanent is to write it to a database. A quick example but you need to do the database code
Default.aspx Markup
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
Default.aspx Code behind
protected void Button1_Click(object sender, EventArgs e)
{
string messageToWriteToTestDiv = TextBox1.Text;
// Code: to write messageToWriteToTestDiv to the database
}
Test.aspx Code Behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string myMessage = "";
// Code: string myMessage = Get Message from Database to write to div
MyTestDiv.InnerText = myMessage;
}
}
Test.aspx Markup
<div id="MyTestDiv" runat="server">
</div>
I have an asp page with some Textbox controls on it.
By default, the browser will suggest previously entered values for each box.
I'd like to prevent that behavior for some of the textboxes.
Is there a way to reliably do that across all major browsers?
I've tried setting
AutoCompleteType="Disabled"
But that seems to have no effect in Firefox.
Here is an image of the behavior I'm trying to prevent.
For firefox
Either:
<asp:TextBox id="Textbox1" runat="server" autocomplete="off"></asp:TextBox>
Or from the CodeBehind:
Textbox1.Attributes.Add("autocomplete", "off");
Autocomplete need to set off from textbox
<asp:TextBox ID="TextBox1" runat="server" autocomplete="off"></asp:TextBox>
By making AutoCompleteType="Disabled",
<asp:TextBox runat="server" ID="txt_userid" AutoCompleteType="Disabled"></asp:TextBox>
By setting autocomplete="off",
<asp:TextBox runat="server" ID="txt_userid" autocomplete="off"></asp:TextBox>
By Setting Form autocomplete="off",
<form id="form1" runat="server" autocomplete="off">
//your content
</form>
By using code in .cs page,
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
txt_userid.Attributes.Add("autocomplete", "off");
}
}
By Using Jquery
<head runat = "server" >
< title > < /title> < script src = "Scripts/jquery-1.6.4.min.js" > < /script> < script type = "text/javascript" >
$(document).ready(function()
{
$('#txt_userid').attr('autocomplete', 'off');
});
//document.getElementById("txt_userid").autocomplete = "off"
< /script>
and here is my textbox in ,
<asp:TextBox runat="server" ID="txt_userid" ></asp:TextBox>
By Setting textbox attribute in code,
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
txt_userid.Attributes.Add("autocomplete", "off");
}
}
This is the answer.
<asp:TextBox id="yourtextBoxname" runat="server" AutoCompleteType="Disabled"></asp:TextBox>
AutoCompleteType="Disabled"
If you still get the pre-filled boxes for example in the Firefox browser then its the browser's fault. You have to go
'Options' --> 'Security'(tab) --> Untick
'Remember password for sites and click on Saved Passwords button to delete any details that the browser has saved.
This should solve the problem
Trying from the CodeBehind:
Textbox1.Attributes.Add("autocomplete", "off");
Adding autocomplete="new-password" to the password field did the trick. Removed auto filling of both user name and password fields in Chrome.
<input type="password" name="whatever" autocomplete="new-password" />
Please note that for Chrome to work properly it needs to be autocomplete="false"
This works for me
<script type="text/javascript">
var c = document.getElementById("<%=TextBox1.ClientID %>");
c.select =
function (event, ui)
{ this.value = ""; return false; }
</script>
i am trying to use javascript events in asp.net webforms. but events for input controls like textfield, such as onClick, onFocus,onBlur, dont appear. do i need to change my directive:
<%# Page Title="" Language="C#" MasterPageFile="~/YourGuruMaster.master" AutoEventWireup="true" CodeFile="AskQuestion.aspx.cs" Inherits="AskQuestion" %>
i want to be able to do this:
//code page
protected void Page_Load(object sender, EventArgs e)
{
QuestionTextBox1.Attributes["onfocus"] = "ClearSearchText()";
//Markup page
function ClearSearchText() {
var searchUserName = document.getElementById('<%=QuestionTextBox1.ClientID%>');
if (searchUserName.value = searchUserName.defaultValue) {
searchUserName.value = "";
}
return false;
}
<p dir="rtl" style="">
<asp:TextBox ID="QuestionTextBox1" runat="server" Width="702px"
Text="פרטים עד 5000 תווים"></asp:TextBox>
Add onfocus and onblur into the markup as follows:
<asp:TextBox ID="TextBox1" runat="server" onfocus="TextBox1_focus(this, event)" onblur="TextBox1_blur(this, event)" Text="Search..."></asp:TextBox>
<script type="text/javascript">
var searchText = 'Search...';
function TextBox1_focus(sender, e) {
if (sender.value == searchText)
sender.value = '';
}
function TextBox1_blur(sender, e) {
if (sender.value == '')
sender.value = searchText;
}
</script>
Well, not sure which ASP.NET version you use. I think last versions allow this (rendering attributes that the server controls don't understand to the browser still). Try using "onfocus" instead (lower case).
However, if this is not working for you, then you have to do it from code behind...
protected void Page_Load(object sender, EventArgs e)
{
QuestionTextBox1. Attributes["onfocus"]="someJavaScriptMethod";
}
Alternatively, if you have jQuery in the page you can go something like ...
<script type="text/javascript">
$(function() {
$('#<%= QuestionTextBox1.ClientID %>').focus(someJavaScriptMethod);
});
</script>
If you do that, inside someJavaScriptMethod(), you can use the word this to point at the focused control, and you can create a jQuery object from it easily like $(this).
.
Please leave me a comment if none of the above solves your problem.