I'm trying to upload file in tow scenarios
First:
<input id="File2" runat="server" name="name" type="file" clientidmode="Static" />
<asp:Button ID="Button4" runat="server" clientidmode="Static"
Text="Go CodeBehind To Get Input Value" OnClick="btnUploadClick" />
This works correctly and the postedFile not null in code behind C#
protected void btnUploadClick(object sender, EventArgs e)
{
HttpPostedFile postedFile= Request.Files[0];
}
Second:
I want to change the browse button "text" and I know already , can't do that directly so i worked around it like that:
<b>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent" >
<input id="File1" runat="server" name="name" type="file" clientidmode="Static" onchange="setHiddenValue()" style=" visibility:hidden;" />
<br />
<input id="Button2" type="button" clientidmode="Static" onclick="triggerFileUpload()" value="HTML Button" />
<br />
<asp:Button ID="Button3" runat="server" clientidmode="Static" Text="Go CodeBehind To Get Input Value" OnClick="btnUploadClick" />
<script language="javascript">
function triggerFileUpload() {
document.getElementById("File1").click();
}
</script>
</asp:Content>
<b>
protected void btnUploadClick(object sender, EventArgs e)
{
HttpPostedFile postedFile= Request.Files[0];
}
When I press the Button2 the fileDialog open, I select file and everything Ok.
But when I press Button3 to get file in server side c# the Request.Files[0] is null
and Found no file posted.
I want the Request.Files[0] because I want save it in database as byte
so please if u have any idea I'll appreciate it
Thank You in advance
I did the same code (second scenario) in a blank project, and this works fine. You could do the same and check it.
Probably something around this slice of code is breaking this. Check if masterpage or current page Page_Load() method have something that break the file 'postback'. You can try:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Page_Load code
}
}
Related
Consider the following code from behind an aspx page:
protected void onBtnClick(object sender, EventArgs e)
{
}
Is it possible to create/craft a POST request (HTTPWebRequest/HttpClient) to call the event handler behind of the aspx page ?
Thanks
Out of curiosity I was able to get this to work, but it takes specific changes to your .aspx page that you probably do not want to do (or cant do): <%# Page EnableEventValidation="false" also have to set your ClientIDMode to static
Given this Test.aspx webform markup:
<form id="form1" runat="server">
<asp:Button runat="server" OnClick="OnClick" Text="Click Me" ClientIDMode="Static" ID="Button1" />
</form>
And this codebehind:
protected void OnClick(object sender, EventArgs e)
{
Response.Write("clicked");
}
I was able to post to Test.aspx from an arbitrary html page (same site) and have OnClick fire, also Page.IsPostBack was set to true:
<form method="post" action="Test.aspx">
<input type="hidden" name="Button1" value="Click+Me" />
<input type="hidden" name="__VIEWSTATEGENERATOR" value="" />
<input type="hidden" name="__VIEWSTATE" value="" />
<input type="submit" value="submit" />
</form>
Again, this is not recommended, probably opens Test.aspx up to all kinds of nasty hacks.
Simple FileUpload example won't work properly on any browser. Instead what it does when the button is clicked is it keeps uploading the same file over and over again.
It seems like this only happens when the file being uploaded is saved under the project path. If I try to save the file on Desktop for example, it works fine.
HTML
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:FileUpload ID="FileUpload1" runat="server" />
</div>
</form>
C#
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string path = Server.MapPath("/Files/");
FileUpload1.SaveAs(path + FileUpload1.FileName);
}
}
I am trying to get html textbox value when click on html button without using "runat" attribute.I need to do it in code behind ,is it possible?
<button id="button1" onclick="btnclick">Click Here</button><input type="text" id="txtBox1" name="txtBox12" />
and My code Behind is like:
protected void btnclick(object sender, EventArgs e)
{
string name = Request.Form["txtBox12"];
Response.Write(name);
}
MODIFIED
To access any control in code behind it needs to have runat=server attribute attached to it.
In your case, like you pointed you can transfer the value of input text to asp hidden field and then access
its value on button click in server-side but in this case too you have to place hidden field inside form runat=server tag.
<form id="form1" runat="server">
<input type="text" id="txt" onkeyup="PassValue(this);";/>
<asp:HiddenField ID="hf" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Go" onclick="Button1_Click" />
</form>
-----------------------
<script type="text/javascript">
function PassValue(obj) {
document.getElementById("hf").value = obj.value;
}
</script>
This is my Default.aspx
<div class="well">
<asp:DropDownList ID="ddlSchools" runat="server"></asp:DropDownList>
<br /><br />
<asp:Button ID="btnContinue" runat="server" Text="Fortsätt" CssClass="btn btn-primary" OnClick="btnContinue_Click" />
<br /><br />
<asp:Image ID="loadingImage" ImageUrl="Images/ajax-loader.gif" runat="server"/>
</div>
I set loadingImage.Visible = false in Page_Load, then i want to show the loadingImage when i hit btnContinue, this is the method i call in Default.aspx.cs, i
protected void btnContinue_Click(object sender, EventArgs e)
{
loadingImage.Visible = true;
ApiHandeler.getSchoolData();
Response.Redirect("Overview.aspx");
}
However the image is still hidden. What am i doing wrong?
You perform redirect to another page right after you set the image visibility. The image would be visible if you stayed on the same page. I think you need to set style="display:none" and handle the client side onclick like $("#loadingImage").show(); - in that case you can use simple HTML image <img src="..." /> without runat="server"
I'm using asp.net and c# to build a website that can upload,edit,save the file. I have use tinymce as the texteditor but the problem is the I dint know how to configure the save plugins in tinymce..I have add asp button to use as save button but how to configure it to save into file .html?
javascript to replace tiny with texeditor:
$('#<%=btnsave.ClientID%>').show() //save button
tinymce.init({selector: "textarea"}); //replace texteditor
this is markup code:
<textarea name="textarea" cols="100" rows="5" style="visibility: hidden;">
</textarea>
<br /><asp:Button Text="Save" runat="server" ID="btnsave" style="display:none;" onclick="btnsave_Click" />
This in code behind
protected void btnsave_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/WordExcelPPointToHtml/test.html");
File.WriteAllText(path,HtmlTextArea); //this line have error!!
}
Can anyone help me on this ?. Thanks in advance. Eagerly waiting for answers.
You need to replace the html control with an ASP.NET web control, and they you will be able to access the value programmatically in the code behind.
E.g.
<asp:TextBox ID="txt" Name="textarea" Columns="100" Rows="5" TextMode="MultiLine" runat="Server"></asp:TextBox>
And in the code behind:
protected void btnsave_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/WordExcelPPointToHtml/test.html");
File.WriteAllText(path, txt.Text);
}
You should also consider some error handling in btnSave_Click