Hi i need to display dialog box on linkbutton click in asp.net.On this dialog there will be file upload control to browse files.
Can you please suggest me any example.Because i am pretty new in development.
Ok, The way you must do is :
1) create a page that has a File Upload control inside it (name it for exmaple MyDialogPage.aspx)
and in code behind
protected void btn1_click(object sender, EventArgs e)
{
//do ur stuff with file
}
2) on the Calling page do this:
<asp:LinkButton Id="callDiag" runat="server" onClientClick="CalljavascriptFunction();return false"/>
3) write this javascript function on the Calling page:
<script>
function CalljavascriptFunction()
{
var url="MyDialogPage.aspx";
var retval = window.showModalDialog(url, window, "center:yes;dialogWidth:800px;dialogHeight:600px");
}
</script>
regards
Related
I'm trying to upload a file with Button element using hidden FileUpload element with ASP.NET using this snippet:
protected void Page_Load(object sender, EventArgs e)
{
Button2.Attributes.Add("onclick", "document.getElementById('" + FileUpload1.ClientID + "').click();");
}
My Button has Id = "Button2", FileUpload Id = "FileUpload1". Clicking on the button Windows Explorer opens successfully to upload files but FileUpload.HasFile still returns false in further code (no file gets loaded even though explorer gets opened and file is selected).
What is the cause of this problem and how to fix it?
Well, I suppose you want to hide the the FileUpload Control and still be able to upload the files.
Follow the following steps to achieve the required:
Place a FileUpload Control with the class hidden. Like below:
<style>
.hidden{
display: none;
}
</style>
<asp:FileUpload runat="server" ID="FileUpload1" CssClass="hidden" />
Then place a Button that will be visible to client and will handle the Javascript events
<asp:Button runat="server" ID="Button1" Text="Upload File" OnClick="Button1_Clicked" />
Add the Javascript events to handle things
$(document).ready(function(){
// This event will help click the FileUpload control
$('#<%= Button1.ClientID %>').on('click', function(){
$('#<%= FileUpload1.ClientID %>').click();
return false; // important to return false, so it does not posts back yet.
});
$('#<%= FileUpload1.ClientID %>').on('change', function(){ // To see if the file selection was changed and the user has selected something
if($(this).val() == ''){
alert('No file selected');
return false;
}
if(confirm('Do you want to upload this file? ' + $(this).val())){
__doPostBack('<%= Button1.ClientID %>', ''); // this will postback to the server click event
}
});
});
Now, handle the uploaded file.
public void Button1_Clicked(object sender, EventArgs e)
{
// Handle your upload function
var hasFile = FileUpload1.HasFile;
}
PS: It was a rough Idea, the code is not tested so it might need some love.
I am using asp control SlideShowExtender in a aspx page.
I am using the previous and next buttons.
I want to add a delete button. Is there a way to find out which picture the user is currently on? I'm having a hard time finding how to get the name of the image SlideShowExtender is currently showing.
Thanks for the help =)
$find("<%= slideshowextend1.ClientID %>")._currentValue.ImagePath;
$find("<%= slideshowextend1.ClientID %>")._currentValue.Name;
Crazy workaround
Add MouseDown event to image (OnClick would not work)
asp:Image runat="server" ID="MainImage" Width="1000" ImageUrl="" onmousedown="javascript:return MainClick();"
JavaScript
function MainClick() {
i = document.getElementById('<%=MainImage.ClientID %>');
c = document.getElementById('<%=MainImageClick.ClientID %>');
c.value = i.getAttribute("src");
__doPostBack(null, null);
return true;
}
</script>
C# - there is probably some way of setting the sender but I am working on a over budget project
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
string mainImageClick = MainImageClick.Value;
if (!string.IsNullOrEmpty(mainImageClick))
DoMainImageClick(mainImageClick);
MainImageClick.Value = string.Empty;
}
}
I have an asp.net application, where the user would click a button and launch another page (within the same application). The issue I am facing is that the original page and the newly launched page should both be launched.
I tried response.redirect, but that tends to unload the original page.
Any suggestions?
This button post to the current page while at the same time opens OtherPage.aspx in a new browser window. I think this is what you mean with ...the original page and the newly launched page should both be launched.
<asp:Button ID="myBtn" runat="server" Text="Click me"
onclick="myBtn_Click" OnClientClick="window.open('OtherPage.aspx', 'OtherPage');" />
Edited and fixed (thanks to Shredder)
If you mean you want to open a new tab, try the below:
protected void Page_Load(object sender, EventArgs e)
{
this.Form.Target = "_blank";
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Otherpage.aspx");
}
This will keep the original page to stay open and cause the redirects on the current page to affect the new tab only.
-J
If you'd like to use Code Behind, may I suggest the following solution for an asp:button -
ASPX Page
<asp:Button ID="btnRecover" runat="server" Text="Recover" OnClick="btnRecover_Click" />
Code Behind
protected void btnRecover_Click(object sender, EventArgs e)
{
var recoveryId = Guid.Parse(lbRecovery.SelectedValue);
var url = string.Format("{0}?RecoveryId={1}", #"../Recovery.aspx", vehicleId);
// Response.Redirect(url); // Old way
Response.Write("<script> window.open( '" + url + "','_blank' ); </script>");
Response.End();
}
Use an html button and javascript? in javascript, window.location is used to change the url location of the current window, while window.open will open a new one
<input type="button" onclick="window.open('newPage.aspx', 'newPage');" />
Edit: Ah, just found this: If the ID of your form tag is form1, set this attribute in your asp button
OnClientClick="form1.target ='_blank';"
You should use:
protected void btn1_Click(object sender, EventArgs e)
{
Response.Redirect("otherpage.aspx");
}
We are running following javascript function:
function btn_AddToList_Click() {
var filePath = document.getElementById("FileUpload").value;
if(filePath.length > 0)
{
var opt = new Option(filePath,filePath);
var listBox = document.getElementById("ListBox");
listBox.options[listBox.options.length] = opt;
}
}
Function binding:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
btn_AddToList.Attributes.Add("onclick", "btn_AddToList_Click(); return false;");
}
}
HTML:
asp:FileUpload ID="FileUpload" runat="server" Width="394px"
asp:ListBox ID="ListBox" runat="server" Width="394px"
asp:Button ID="btn_AddToList" runat="server" Enabled="true" Text="Add"
Issue is that value of "FileUpload" is not get cleared after we click "Add" button. Any help?
You can not set/clear the value of FileUpload control programmatically. That is a restriction for a security reason. Consider this if this restriction was not there, you could set the value of FileUpload control to some arbitrary file and upload it to your server. You won't be able to achieve this in current shape.
As a work around you can try to bring another textbox exactly on top of textbox part of FileUpload control. This way you will be give the same feeling what you are trying to achieve. But that is also not ideal and may not work properly.
I have a LinkButton that has to postback to perform some logic.
Once it is finished, instead of loading the page back up in the browser, I want to leave it alone and pop open a new window.
So far, the best idea I've had is to put the LinkButton in an UpdatePanel, and have it render some JavaScript out when it reloads, yet I think that is totally hacky. Also, if I recall right, JavaScript within a update panel won't run anyways.
Any other ideas?
Use LinkButton.PostBackUrl to set a different page to POST to, and some client script to get a new window (and the old target restored so that future postbacks work normally). The 2nd page can use PreviousPage to get access to any needed state from the original page.
<script runat="server">
void lnk_Click(object sender, EventArgs e) {
// Do work
}
</script>
<script type="text/javascript">
var oldTarget, oldAction;
function newWindowClick(target) {
var form = document.forms[0];
oldTarget = form.target;
oldAction = form.action;
form.target = target;
window.setTimeout(
"document.forms[0].target=oldTarget;"
+ "document.forms[0].action=oldAction;",
200
);
}
</script>
<asp:LinkButton runat="server" PostBackUrl="Details.aspx" Text="Click Me"
OnClick="lnk_Click"
OnClientClick="newWindowClick('details');" />
Here is the code:
protected void Button1_Click(object sender, EventArgs e)
{
// Do some server side work
string script = "window.open('http://www.yahoo.com','Yahoo')";
if (!ClientScript.IsClientScriptBlockRegistered("NewWindow"))
{
ClientScript.RegisterClientScriptBlock(this.GetType(),"NewWindow",script, true);
}
}
One thing you could try is to have your LinkButton OnClick event do its processing, then register a Page.ClientScript.RegisterStartupScript with the popup code, which will put some Javascript into the tag to fire off after the page loads. This should launch your new window after the processing completes.
EDIT: Reading your comment, I believe you can still use this approach, have your results stored in a session variable, and then have the popup page pull the results from there.