C# / ASP.NET - Override Browse Event handler on FileUpload control? - c#

I'm currently using an ASP.NET File Upload control. This works great, but I'd like to remove the current Upload button and put its logic into the Browse... button instead.
Is there a way to overload the Browse... button's logic?
Currently:
I'd like to have this:
When the user clicks the Open button in the File Upload box, the Upload buttons' logic fires:
Upon clicking Open, the following code triggers in C# Code behind.
protected void override btnSomeOverriddenControl_Click(object sender, EventArgs e)
{
if (multipleFile.HasFiles)
{
foreach (var file in multipleFile.PostedFiles)
{
//do stuff
}
}
}

Related

How to refresh a user control every time a button is clicked

I have a series of menu options which are all individual user controls on a windows form application.
How do I refresh the user control so that if for example I added a new person to my txt file, when I click the Birthdays button, it preforms all the functions within the Birthday User control again on the file with the new person added.
What's happening now is when I add a new person to my txt file, The user controls don't refresh therefore the Data.updatedata() method isn't called and the data is not updated.
Is there a particular event or method that I could use in order to refresh the user control when clicked?
I have tried using birthdayUserControl1.refresh() in the main form
namespace Project
{
public partial class ChildrenUi : Form
{
public ChildrenUi()
{
InitializeComponent();
homeUserControl1.BringToFront();
}
private void button2_Click(object sender, EventArgs e)
{
birthdaysUserControl1.Refresh();
birthdaysUserControl1.BringToFront();
}
}
}
I have only just started learning about Winforms and came across Data Binding using XAML/XML files on similar questions regarding refreshing user controls however I don't know much about XAML/XML and I would imagine i'd have to redesign a good portion of my project to facilitate that. I'm using a text file.
Refreshing whole birthdaysUserControl1 won't refresh inner ListBox datasource, you need to manually refresh it.
private void button2_Click(object sender, EventArgs e)
{
birthdaysUserControl1.RefreshList();
}
And inside birthdaysUserControl1:
public void RefreshList()
{
listbox1.DataSource=null;
listbox1.DataSource=UpcominBdays;
}
To watch the contents of your textfile, you can use the System.IO.FileSystemWatcher class. The Changed-Event informs your application whenever the content of the watched file is changed.

Pass "enter" from text box to another text box

I want to create 2 simple textbox in aspx file and .cs, whenever I click a button, it will pass the value to another textbox. as simple as this:
private void Click(object sender, EventArgs e)
{
textbox1.text=textbox2.text;
}
However, the "enter" message doesn't pass to another textbox. please view the example below
do you have any idea how to do it?
You will need to do that in the button's click event, say you have a button called transfer, then create a click event called
private void Transfer_OnClick(......)
{
textbox1.text=textbox2.text;
}
That should be all you need, this is assuming you are using ASP.NET Webforms.

Button not firing in user control

I currently have a master page that has a drop down menu which displays a different User control depending on the menu choice.
Inside of the User control I'm trying to add a submit button that processes the inputted forms inside the user controls using the "_Click()" event handler. My user control aspx looks like the following:
<asp:Button ID="configBttn" runat="server" OnClick="configBttn_Click" AutoEventWireUp="true" />
and then I have the following in the codebehind:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void configBttn_Click(object sender, EventArgs e)
{
Response.Write("Hello world");
}
I have even tried to add an event handler:
configBttn.Click += new EventHandler(configBttn_Click)
However this is still not allowing for the button to be triggered. Any ideas?
I add the user control inside the codebehind inside a div container:
divContainer.Controls.Add(pPage.LoadControl("~/Controls/Control.ascx"));
Where are you creating the UserControl in the Page Lifecycle?
I suspect that it needs to be created in the Init method of its container page.
In addition, they should not be wrapped in a !IsPostback block, or else when the page posts back, the control event handler is not mapped, so nothing is there to catch the click event.
Without seeing the code that adds the user control, I'm guessing that it is not saved as part of the page's view state. Which means when you click the button inside the user control the whole page gets refreshed and the user control is either not created or a different instance of it is created.

How to check if button from user control was clicked?

It seems like a straight forward thing, but I can't figure it out.
How can I check if a certain button was clicked from user control?
my user control is uc_test and button name is btnTest .
I assume its some sort of event handler added to uc_test.btnTest ?
I'm working in WinForms.
It sounds like you've created a button but want to make it do something on click. I don't often use Winforms but from memory double-clicking on the control in the form should automatically create a btnTest_Click method. If not, just go to code and put in a method like:
protected void btnTest_Click(object sender, Eventargs e)
{
//do something
}
Then set in the properties of the button the OnClick event to btnTest_Click.

Creating an event for a textbox and AJAX Update Panel Control

I wanted to create a "Click" event for a textbox in C# (as there isn't any).
So, this way
protected void Page_Load(object sender, EventArgs e)
{
if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "txt1OnClick")
{
txt1_Click();
}
txt1.Attributes.Add("onclick", this.Page.ClientScript.GetPostBackEventReference(txt1, "txt1OnClick"));
}
private void txt1_Click()
{
ImageMap1.ImageUrl = "guide/1.jpg";
}
Then I wanted to load the image without reloading the page.
So I used the AJAX UpdatePanel Control and this worked fine with
protected void Button1_Click(object sender, EventArgs e)
{
ImageMap1.ImageUrl = "guide/1.jpg";
}
But not with the event I created, because the compiler doesn't identify my new events as
a real event or something I couldn't figure out.
I added the button1_click event according to Step 8 of "Refreshing an UpdatePanel Control with an External Button".
The click event of textbox is not shown in this option:
So my question is is there any way to add this event within System.Web.UI.WebControls.TextBox class or, to make this event visible within the above option?
So that I can include click event of the textbox within the Triggers of the update panel.
If you try to create a Click event for a TextBox, every time a user clicks your textbox you'll trigger a postback to the server (even to evaluate if you need to do something as part of handling the event). This is very inefficient - you should handle clicks in the browser, using JavaScript and then trigger the UpdatePanel using client-side logic.
This lets you trigger a call to the server if you need it but avoid it when you don't. If you have a server-side event handler, your code will post back to the server (reloading the page) every time the user clicks the TextBox.
You can read this link (and others) about using __doPostBack() on the client side to trigger an UpdatePanel to perform a postback.
http://encosia.com/easily-refresh-an-updatepanel-using-javascript/

Categories

Resources