change label text programmatically in c# web forms - c#

I searched the internet over and over, and this seems to be very straightforward, but somehow it's not working.
I'm trying to create a web forms application in C#.
Button1 is supposed to download a lot of data from a website, and I want to Label1 to display "downloading" while the code in Button1 runs, and "done" after the code finishes, but Label1 is not changing at all after button click.
code is below:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "downloading";
//code for downloading data
Label1.Text = "done";
}

That's not going to work. That's all server-side code. The updated HTML is not sent back to the browser until after the Button1_Click handler is finished executing. So the browser will only see the "done" label text. Never the "downloading" text.
The easiest way to achieve your desired effect is to use client-side javascript (maybe using jquery) to update the label text to "downloading" before you submit the form. You can put your client-side javascript in the OnClientClick property of the asp.net button. The server-side code can then download the data and change the label text to done.

I don't know if this will still be helpful since the post is rather old, but I stumbled upon the question while searching for an answer to a question of my own.
Just use the Label1.Update() function after you change the label's text.

If you want to change text from downloading to done then you may do this:
Label lbl = (Label)this.FindControl("Label1");
if (lbl != null)
{
lbl.Text = "done";
}

protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "downloading";
//code for downloading data
Label1.Text = "done";
Label1.Refresh;
}

Related

How to change text box text by click on button

Hello I am beginner in asp.net webforms and i want to copy text of textbox in another textbox when i click in button
my code is :
protected void Button2_Click(object sender, EventArgs e)
{
TextBox15.Text = TextBox6.Text;
}
when i fill TextBox6 and click button nothing show in TextBox15
Try to use IsPostBack property in the pade_load event handler to prevent re-validation of the page every time you click the button.
if (!IsPostBack)
{
//do nothing
}
By the way code of your button click will remain as it is, dont change anything of it, just write the above code on the page load.
I hope it helps

How to set a link's NavigateUrl on link onClick?

I'm creating a link dynamically. The NavigateUrlproperty of the link is set on a call to a method. But I don't want to call the method before the link is clicked because there are so many of such links.
Any help on how to do it is appreciated. Thanks.
public void CreateLink()
{
LinkButton link = new LinkButton();
link.Click += new EventHandler(link_Click);
string key = GetKey();
}
private void link_Click(object sender, EventArgs e)
{
var url = GetLinkUrl(e.???);
Response.Redirect(url);
}
public string GetLinkUrl(string key)
{
//do things to retrieve url
return url;
}
Update: Many thanks, all :) I am going to use LinkButton, as seen in updated code above.
But I forgot to ask: There is a key associated with each link which is needed to get the URL. How can I
What you're doing is potentially quite complex; you would need to output back to the client something indicating a call to a code-behind method to be executed before the navigation occurs, for example:
HyperLink link = new HyperLink();
link.Attributes.Add("onclick", "doClientsideCode();");
...
Then, a bit of JavaScript:
function doClientsideCode() {
// Do a call to a service or similar, to run the method you're wanting to run.
// This will then do the navigation you require.
}
Thus, my suggestion would actually be a LinkButton; this will fire an event that you can capture server-side, via something similar to:
public void link_Click(object sender, EventArgs e) {
// Run my method
// Issue response.redirect to correct URL.
}
[Edit - in answer to the edited question, you have access to the properties CommandName and CommandArgument when using a LinkButton which should do what you require. However, I do feel that that part should now be a separate question.]
One alternative is to use an ASP button instead of Hyperlink, and bind the GetLinkUrl function inside a click handler for the button. Then run Response.Redirect('yourURL') to execute the link.
EDIT: As Adrian said above, a LinkButton will be the best of both worlds - it is styled like a Hyperlink, but you can set a click handler for it to get the URL dynamically, only when the button is actually clicked.

how to see DB changes without refreshing page in ASP.NET C#

I have this button event:
protected void AddDetails_Click(object sender, EventArgs e)
{
DataSetTableAdapters.SelectFriendsTableAdapter sft = new DataSetTableAdapters.SelectFriendsTableAdapter();
try
{
sft.AddFriend(current, newFriend, false);
}
catch
{
error.Text = "Something happened. Bummer!";
}
}
in the try section, I'm adding entries in the Database. In the page there are Labels / Textboxes with the corresponding values.
Everything works fine. However, I need to refresh the page in order to see the changes after I click on this submit button.
I have added if(!IsPostBack) at the beginning of the PageLoad code, but I still need to visit another page / come back to see the changes.
Any ideas?
Thanks.
Thank you for your replies. I'm using a ListView:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSetTableAdapters.SelectFriendsTableAdapter sdt = new DataSetTableAdapters.SelectFriendsTableAdapter();
DataTable tab = sdt.SelectFriends();
ListView1.DataSource = tab;
ListView1.DataBind();
}
}
, so the ListView content should get updated.
If you don't want to refresh the page and you're using asp.net webforms you can use an UpdatePanel and place all your controls inside of it, etc... That will keep the changes made in the form / display when you submit the info. I'm assuming the form is on submitting the changed/new data as you didn't specifically state how the data was being updated/changed.
If you don't want to use an update panel, then when the page is posting back you will have to set the values for the UI controls that you want to update with the values from the 'newFriend' object (I'm assuming that has the changed values).
Found it!
The solution for this issue is to add this code at the end of your submit button click event:
Server.Transfer("currentpage.aspx");

Submission of a webpage form using WebBrowser control in C#

I have seen a lot of posts regarding this particular subject on SO as well as on the web in general and most if not all code is as seen below
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
webBrowser1.Navigate(new Uri("http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onsubmit.htm"));
}
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
mshtml.HTMLDocument htmlDoc = null;
htmlDoc = (mshtml.HTMLDocument) this.webBrowser1.Document;
if (webBrowser1.Document != null)
{
foreach (mshtml.HTMLFormElement form in htmlDoc.forms)
{
form.submit();
break;
}
}
}
The code has no errors whatsoever but for the life its not submitting. The sample page that I am using has simple button, what it does, it alerts the selection of the radio button and then submits the form. For some strange reason when the form is submitted via code using the WebBrowser control, the form is submitted but the alert never shows up.
I am not sure what I am doing wrong here. Any help on this would be appreciated.
Would performing a click on the button do what you need it to do? You will need to add a COM reference to the Microsoft HTML Object Library (which you may already have). For example, if you load up google into the webbrowser control, this code will place "hello world" into the search box and perform the search:
mshtml.IHTMLDocument2 doc = ((mshtml.HTMLDocumentClass)webBrowser1.Document);
((mshtml.IHTMLElement)doc.all.item("q")).setAttribute("value", "hello world");
MessageBox.Show("Clicking I'm feeling lucky button");
((mshtml.HTMLInputElement)doc.all.item("btnI")).click();
Edit: I updated the code for the components that the WPF WebBrowser control uses. Also note that this sometimes throws a script error from google, but that appears to be a timing issue based on some of the ajax calls google has on the home page.
To fix your problem you need to replace line:
form.submit();
With following code:
var children = form as IEnumerable;
var inputs = children.OfType<mshtml.HTMLInputElement>();
var submitButton = inputs.First(i => i.type == "submit");
submitButton.click();
This will show alert about user selection and submit form.
I've got a more dirty one-liner working by injecting a JavaScript to submit the form
_webBrowser.InvokeScript("eval", new object[] { "document.getElementById('formName').submit()" });
That's been working for me when interacting with a site using a lot of JavaScript and button beyond the form.

List replies in a winform

In my C# app I get an xml from a server that contains some replies like in a forum thread (with elements like author, time, body, title, whatever).
When I get this xml, I create a new form in which i want to display these replies, and a little text box with an "add reply" button. I'd also like some edit buttons on perhaps my own replies in the reply list displayed in the form.
The simplest way that came to my mind to display the replies is to put a web browser control in the form, generate a full html page in a string from the xml, and throw it in that web browser control. And under it i can put the text box with the add reply button.
Everything is ok, except that i have no idea of how i could implement the edit function on my own replies (i mean i could add a link in there... but link to what)
I would like to know if there is a way to get that edit event from the web browser control (my guess is i can't) or another (maybe simple/easy) idea of displaying the replies in a winform using other controls
Yes, that's possible, you want to turn "design mode" on for the document. Add a reference to Microsoft.mshtml. Start a new Windows Forms project and drop a WB and a button on the form. Make the code look similar to this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
webBrowser1.DocumentText = "<html><body><textarea rows='15' cols='92' name='post-text' id='wmd-input'></textarea></body></html>";
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
button1.Click += button1_Click;
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
mshtml.IHTMLDocument2 doc = webBrowser1.ActiveXInstance as mshtml.IHTMLDocument2;
doc.designMode = "On";
}
private void button1_Click(object sender, EventArgs e) {
var html = webBrowser1.Document.Body.All["post-text"].InnerHtml;
// do something with that
//...
}
}

Categories

Resources