Here is ASP.NET code:
<div class="row1" style="padding: 3px">
<asp:Button Text="Select" ID="btnDescColumn" runat="server" OnClick="SetDescPoint" CausesValidation="False"/>
<input type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" id="fileSelect" runat="server" class="hidden" />
<asp:Button ID="btnUpload" runat="server" Text="Load" OnClick="LoadFile" class="hidden" CausesValidation="False" />
<input type="button" id="triggerUpload" name="name" value="Select File" />
</div>
Here the view:
Here is JQuery code:
$('#triggerUpload').click(function () {
$('#<%=fileSelect.ClientID%>').trigger('click');
});
$('#<%=fileSelect.ClientID%>').change(function () {
$('#<%=btnUpload.ClientID%>').trigger('click');
});
When Select File button clicked dialog window opens and user select file.
After the file selected, this JQuery code fired:
$('#<%=fileSelect.ClientID%>').change(function () {
$('#<%=btnUpload.ClientID%>').trigger('click');
});
and the JQuery row above trigger this asp button control:
<asp:Button ID="btnUpload" runat="server" Text="Load" OnClick="LoadFile" class="hidden" CausesValidation="False" />
which is fire this code behind method:
protected void LoadFile(object sender, EventArgs e)
{
HttpPostedFile file = Request.Files[fileSelect.Name];
int fileSize = file.ContentLength;
byte[] fileByteArray = new byte[fileSize];
file.InputStream.Read(fileByteArray, 0, fileSize);
}
After the postBack Process button clicked and this code behind is fired:
protected void SetDescPoint(object sender, EventArgs e)
{
if(fileSelect.Value != string.Empty)
{
//make some process...
}
}
But the fileSelect control is empty.
As I understand input file not keep the file between postback calls.
I need to access the file in SetDescPoint code behind.
Any idea how can I keep file between postback calls?
When you uploading a file in asp.net .. you must do a full post-back .. which can only achieve by submitting the page (button click).. no jquery trick will work .. even if your postback button is under a updatepanel .. then also you will not get the physical file in server side
Related
I hava a MasterPage with 2 UserControls. When something happens in UserControl1.ascx, it has to update a TextBox in UserControl2.ascx.
I tried this inside UserControl1.ascx, but no success:
UserControl userControl = (UserControl)LoadControl("UserControl2.ascx");
var txt = (TextBox) userControl.FindControl("txtTest");
txt.Text = "Hello world";
Thanks for all help :)
The LoadControl is load a new control that is not exist on page...
Lets see this example.
You have a master page that has 2 User Controls.
<form id="form1" runat="server">
<div>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
<br /><br />
<uc2:WebUserControl ID="WebUserControl2" runat="server" />
<br /><br />
<asp:Button runat="server" ID="btnOk" Text="ok" OnClick="btnOk_Click" />
</div>
</form>
and on code behind this
protected void btnOk_Click(object sender, EventArgs e)
{
WebUserControl1.TextOnMe = WebUserControl2.TextOnMe;
}
Now the user controls have this on html
<asp:TextBox runat="server" ID="txtText"></asp:TextBox>
and on code behind you get and set the Text like that on code behind
public string TextOnMe
{
get
{
return txtText.Text;
}
set
{
txtText.Text = value;
}
}
One Similar Answer
ASP.NET MasterPage only control?
i need to request file from fileupload that is created in html. my question is quite simple how do i do this?
i know there is this option : HttpPostedFile File = Request.Files["imagem"]; but when i try to do that my File returns NULL.
I dont know what am i doing wrong, but even this simple code example is not working and i dont know why .
<body>
<form id="form1" runat="server">
<div>
protected void Button1_Click(object sender, EventArgs e)
{
HttpPostedFile File = Request.Files["imagem"];
if ( File != null)
Response.Write("Sucesso");
}
</form>
</body>
and here is my aspx code example :
<input type="file" name="image" class="image-upload" /></div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
can any one explain what am i doing wrong ? thank you
Might just be a typo on here, but change your imagem to image
protected void Button1_Click(object sender, EventArgs e)
{
HttpPostedFile File = Request.Files["image"];
if ( File != null)
Response.Write("Sucesso");
}
In your aspx page you need to tell the form that it will be using files. I don't think its required but its good practice to also include an id in your input that matches your name.
<body>
<form id="form1" runat="server" method="post" enctype="multipart/form-data">
<input type="file" id="image" name="image" class="image-upload" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</form>
</body>
I'd recommend going with the asp.net file upload control:
<asp:FileUpload ID="pictureUpload" class="form-control input-sm input-spacing" runat="server" />
You can create these dynamically
FileUpload OneUpload = new FileUpload();
OneUpload.ID = "Upload" + MyIdNumber;
UploadsDiv.Controls.Add(OneUpload);
Then to access the file, you have:
pictureUpload.SaveAs(FilePathAndName);
You can also access different properties of the posted file...
pictureUpload.PostedFile.FileName
pictureUpload.PostedFile.ContentLength
pictureUpload.PostedFile.ContentType
Recently I have been developing web form application in ASP.NET (c#):
I have an Image control:
<asp:Image ID="Avatar" runat="server" Height="225px" ImageUrl="~/Images/NoUser.jpg" Width="225px" />
And FileUpload & Button control
<asp:FileUpload ID="avatarUpload" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
When user click button then "Upload" code is executed (the image is sent to the database). Problem is that I like to display the image which the user selected in Avatar Image controller before the user clicks "desperate" button.
Is that possible to do this automatically?
With the help of File Api of HTML5 (Example: Using files from web applications) you can accomplish this easily. Change the markup to use input type="file" instead of asp:FileUpload and add ID, add tag runat="server" to make it accessible from server. Your markup should look like:
<input ID="avatarUpload" type="file" name="file" onchange="previewFile()" runat="server" />
<%--<asp:FileUpload ID="avatarUpload" runat="server" />--%>
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
<asp:Image ID="Avatar" runat="server" Height="225px" ImageUrl="~/Images/NoUser.jpg" Width="225px" />
Now add a javascript function previewFile in the head of document:
<head runat="server">
<title></title>
<script type="text/javascript">
function previewFile() {
var preview = document.querySelector('#<%=Avatar.ClientID %>');
var file = document.querySelector('#<%=avatarUpload.ClientID %>').files[0];
var reader = new FileReader();
reader.onloadend = function () {
preview.src = reader.result;
}
if (file) {
reader.readAsDataURL(file);
} else {
preview.src = "";
}
}
</script>
</head>
Now after selecting an image you can see the preview like below:
You can use css to re-size it to a thumbnail.
After clicking the Upload button, in the code you can find the posted file:
protected void Upload(object sender, EventArgs e)
{
int contentLength = avatarUpload.PostedFile.ContentLength;//You may need it for validation
string contentType = avatarUpload.PostedFile.ContentType;//You may need it for validation
string fileName = avatarUpload.PostedFile.FileName;
avatarUpload.PostedFile.SaveAs(#"c:\test.tmp");//Or code to save in the DataBase.
}
How to create onclick event in .cs file for asp.net c# for html Tag ?
Example :-
< input type="button" id="btnBeforeOk" value="Ok" name="btnBeforeOk" style="width: 90%;" />
I want to create an event in .cs file how we can do this ?
OR
bool isEmail = Regex.IsMatch("nikunj#yahoo.com", #"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|asia|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel)\b)\Z", RegexOptions.IgnoreCase);
The above bool isEmail i want to in .aspx page ?
Very simple.
HTML:
<input type="button" id="btnBeforeOk" value="Ok" runat="server" name="btnBeforeOk" style="width: 90%;" />
Now go to HTML view of your page and double click on your button, it will generate event for you and button markup will be changed to below:
<input type="button" id="btnBeforeOk" value="Ok" runat="server" name="btnBeforeOk" style="width: 90%;" onserverclick="btnBeforeOk_ServerClick" />
and you can find below event in your code behind:
protected void btnBeforeOk_ServerClick(object sender, EventArgs e)
{
Response.Write("hello");
}
I hope it helps!
I think you should use :
<asp:Button ID="MyButton" runat="server" OnClick="YourEvent" />
And do whatever you want to do in the code behind :
protected void YourEvent(object sender, EventArgs e)
{
//work
}
I am firing an event from an ascx control in order to update some controls in the container to which the ascx control belongs.
The ascx control is displayed via a modal popup extender. When I click a button inside the ascx, I fire an event to which the container containing the ascx control is subscribes.
The event delegate is fired and the expected logic is run in the container's code behind, the problem is that any changes made to controls inside not the container aren't updated despite the event logic having been processed. The expected changes are not reflected on the page when the results of the postback is rendered.
Are there any pitfalls I should know of?
The markup for the container
<asp:Panel ID="panelTreeViewAttributesTitle" runat="server">
<asp:Label ID="someLabel" runat="server" Text="Hello Governor" />
<asp:LinkButton ID="LinkButtonEdit" runat="server" Text="(Edit)" />
<ajax:ModalPopupExtender BackgroundCssClass="modalBackground" Enabled="True"
ID="btnEdit_ModalPopupExtender" PopupControlID="modalPanel" runat="server"
TargetControlID="LinkButtonEdit" />
</asp:Panel>
<asp:Panel ID="modalPanel" runat="server" CssClass="modalPopUp" Style="display: none">
<xxx:customControl runat="server" ID="myCustomControl" />
</asp:Panel>
The code behind for the container
protected void Page_Load(object sender, EventArgs e)
{
myCustomControl.Updated += eventCaptured;
if (IsPostBack) return;
...
}
void eventCaptured(object sender, EventArgs e)
{
someLabel.Text = "Goodbye Governor";
}
The custom control
<script type="text/javascript">
function Update() {
var ajaxManager = $find("<%= RadAjaxManager.GetCurrent(Page).ClientID %>");
if (ajaxManager != null)
ajaxManager.ajaxRequest("");
}
</script>
<asp:Panel ID="panelEditPanel" runat="server">
<asp:Label ID="lblCMA" runat="server" Text="Call me Arnooold." />
</asp:Panel>
<asp:Button ID="btnUpdate" runat="server" Text="Update" OnClientClick="Update()" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
The custom control's code behind
public event EventHandler Updated;
protected void AjaxManager_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
//Some DB backend logic
UpdateFinished();
}
private void UpdateFinished()
{
if (Updated == null) return;
Updated(this, null);
}
Maybe the button click is causing a partial post back instead of a normal, full-page post back. If this is the case, the entire life cycle would run on the server side (including your event handler) but only part of the HTML would be updated on the client side when the response comes back to the browser. "someLabel" could be outside the region that gets updated on the client side.