SlideShowExtender - How to get the current picture? - asp.net

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;
}
}

Related

I need to open dialog box on linkbutton click using asp.net

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

Calendar, Dynamic LinkButton CommandEventHandler Help

I am attempting to create a calendar which is just a simple UI to display dates and dates to users of our system. I have overridden the Calendar's "DayRender" event to gain access to each cell and then insert a couple of dynamic controls to display specific data. The display of the controls works great. However, I recently wanted to add a LinkButton with command arguments and capture the event to run some other logic and change the UI. I have gotten the LinkButton to display properly and it renders as a simple "" tag with the ID that is assigned. However clicking on the link does nothing and it appears that the normal "href='...javascript action...'" portion of the link is not being generated. I have a feeling this is all due to the fact that I am adding the control at the Day Render stage in the page life cycle. But if that was the case the control probably would not show up at all.
Any ideas as to why the click action is not being added yet the text and everything else are? Code is below.
Thanks for your time
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (Schedule != null)
{
var dayReq = from day in Schedule
where day.RequiredDate == e.Day.Date
where day.RequiredQty != 0
select day;
if (dayReq.FirstOrDefault() != null)
{
//Open the Date
e.Cell.Controls.Add(new LiteralControl("<br /><div class=\"auth-sched-req\">Req Qty: <strong>" + String.Format("{0:#,#.###}", dayReq.FirstOrDefault().RequiredQty) + "</strong><br />Prom Date: "));
//Create a link button for the promise date
LinkButton lb = new LinkButton();
lb.ID = dayReq.FirstOrDefault().ItemId.ToString();
lb.Text = dayReq.FirstOrDefault().RequiredDate.ToShortDateString();
lb.CommandName = "ShowPromise";
lb.CommandArgument = dayReq.FirstOrDefault().ItemId.ToString();
lb.Command +=new CommandEventHandler(lb_Command);
e.Cell.Controls.Add(lb);
//Close the Date
e.Cell.Controls.Add(new LiteralControl("</div>"));
}
}
}
protected void lb_Command(Object sender, CommandEventArgs e)
{
//Do some magic here
Response.Write(e.CommandArgument.ToString());
}
try this
<script type="text/javascript">
function calendarClick(day) {
document.getElementById('<%= hfValue.ClientID %>').value = day;
document.getElementById('<%= ghostButton.ClientID %>').click();
}
</script>
<asp:Button ID="ghostButton" runat="server" Style="display: none" OnClick="ghostButton_Click" />
<asp:HiddenField ID="hfValue" runat="server" />
code behind:
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
// ...
e.Cell.Attributes["onclick"] = string.Format("calendarClick({0})", dayReq.FirstOrDefault().ItemId.ToString());
// ...
}
protected void ghostButton_Click(object sender, EventArgs e1)
{
string value = hfValue.Value;
// ...
}
Check if AutoEventWireup is set to true i.e.
AutoEventWireup="true"
I ended up going another route with this request. None of the proposed solutions worked and I never could get it to work. One option we looked at was creating a custom table to build the control. We ended up changing where the information was changed and just used the calendar control as the display mechanism.
Sorry for the delay!

Clearing the value of ASP.Net 2.0 File Upload Control using javascript that works on all browser?

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.

Set focus to textbox in ASP.NET Login control on page load

I am trying to set the focus to the user name TextBox which is inside an ASP.NET Login control.
I have tried to do this a couple of ways but none seem to be working. The page is loading but not going to the control.
Here is the code I've tried.
SetFocus(this.loginForm.FindControl("UserName"));
And
TextBox tbox = (TextBox)this.loginForm.FindControl("UserName");
if (tbox != null)
{
tbox.Focus();
} // if
I'm using Page.Form.DefaultFocus and it works:
// inside page_load, LoginUser is the Login control
Page.Form.DefaultFocus = LoginUser.FindControl("Username").ClientID;
Are you using a ScriptManager on the Page? If so, try the following:
public void SetInputFocus()
{
TextBox tbox = this.loginForm.FindControl("UserName") as TextBox;
if (tbox != null)
{
ScriptManager.GetCurrent(this.Page).SetFocus(tbox);
}
}
Update: Never used a multiview before, but try this:
protected void MultiView1_ActiveViewChanged(object sender, EventArgs e)
{
SetInputFocus();
}
protected void Page_Load(object sender, EventArgs e)
{
SetFocus(LoginCntl.FindControl("UserName"));
}
You may try to do the following:
-Register two scripts (one to create a function to focus on your texbox when page is displayed, second to register id of the textbox)
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "on_load",
"<script>function window_onload() \n { \n if (typeof(idLoginTextBox) == \"undefined\" || idLoginTextBox == null) \n return; \n idLoginTextBox.focus();\n } \n window.onload = window_onload; </script>");
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Focus", String.Format("<script>var idLoginTextBox=document.getElementById(\"{0}\").focus();</script>", this.loginForm.ClientID));
As the result you should get the following in your code:
<script>
function window_onload()
{
if (typeof(idLoginTextBox) == "undefined" || idLoginTextBox == null)
return;
idLoginTextBox.focus();
}
window.onload = window_onload;
</script>
<script>
var idLoginTextBox=document.getElementById("ctl00_LoginTextBox").focus();
</script>
I've been struggling with this too and I've found a solution that seems to work very well even with deeply nested controls (like AspDotNetStorefront a.k.a. ASPDNSF uses). Note the following code called from the Page_PreRender routine. I knew the name of the TextBox I wanted to give focus to and so I just called FocusNestedControl(Me, "UserName"). I just used Me here because all the routine needs is a parent of the control to get focus; it doesn't matter which parent.
Public Function FocusNestedControl(ByVal ParentControl As Control, ByVal ControlNameToFocus As String) As Control
If ParentControl.HasControls Then
For Each childCtrl As Control In ParentControl.Controls
Dim goodCtrl As Control = FocusNestedControl(childCtrl, ControlNameToFocus)
If goodCtrl IsNot Nothing Then
goodCtrl.Focus()
Return goodCtrl
End If
Next
Else
If ParentControl.ID = ControlNameToFocus Then
ParentControl.Focus()
Return ParentControl
End If
End If
Return Nothing
End Function
You can set focus directly on LoginControl and it will automatically set focus on first field in control. In your case:
this.loginForm.Focus();
More info on MSDN: How to: Set Focus on ASP.NET Web Server Controls
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Login.FindControl("UserName").Focus();
}
My problem arrized when i moved login control to a custom control and tried to find UsernameTextBox at the OnInit() method.
OnInit of a control is executed before OnInit of Page and this is why no Form control have been created.
I moved the call to UsernameTextBox to the OnLoad function and it worked correctly.
None of the above answers worked for me, so I simply tried:
protected void Page_Load(object sender, EventArgs e) {
// This works for me
TxtMyTextBoxName.Focus();
}
... and it worked!
With an ASP TextBox defined as:
<asp:TextBox ID="TxtMyTextBoxName" type="search" runat="server"></asp:TextBox>
You must put your textbox inside this code on ASP.NET
<form id="WHATEVERYOUWANT" runat="server" method="post">
<div>
<!-- Put your code here-->
</div>
</form>

window.location.href not working in Safari when using onkeypress

I'm using an asp textbox and a search button. In Safari if I click the search button i get redirected to the search results page using javascript window.location.href. But strangely the same javascript will not redirect to the page if I press return in the textbox.
Using the alert function I can see that window.location.href has the the correct url and the location bar at the top changes from the search page(default.aspx) to the search results url however when I click OK to the alert box the url at the top reverts back to the default.aspx page. It works on ie7/8/firefox/chrome but not safari. Here is my javascript,cs and aspx code:
function submitSearchOnEnter(e) {
var CodeForEnter = 13;
var codeEnteredByUser;
if (!e) var e = window.event;
if (e.keyCode) codeEnteredByUser = e.keyCode;
else if (e.which) codeEnteredByUser = e.which;
if (codeEnteredByUser == CodeForEnter)
RedirectToSearchPage();
}
function RedirectToSearchPage() {
var searchText = $get('<%=txtHeaderSearch.ClientID%>').value
if (searchText.length) {
window.location.href = "Search.aspx?searchString=" + searchText;
}
}
protected void Page_Load(object sender, EventArgs e)
{
txtHeaderSearch.Attributes.Add("onkeypress", "submitSearchOnEnter(event)");
}
<asp:Panel ID="pnlSearch" runat="server" DefaultButton="lnkSearch">
<asp:TextBox ID="txtHeaderSearch" runat="server" CssClass="searchBox"></asp:TextBox>
<asp:LinkButton ID="lnkSearch" OnClientClick="RedirectToSearchPage(); return false;"
CausesValidation="false" runat="server" CssClass="searchButton">
SEARCH
</asp:LinkButton>
</asp:Panel>
I've tried return false; which doesn't allow me to enter any characters in the search box. I've spent ages online trying to find a solution. Maybe it has something to do with setTimeout or setTimeInterval but it didn't work unless i did it wrong.
In Safari, the 'onkeypress' event is only fired if the key press results in a character being added to an HTML element. I'm not sure that this will fire at all when you press Enter in a one-line text box. See a partial explanation of this behavior here.
As Tim Down says, you probably want onkeydown instead.
Use keydown instead of keypress and you should be fine.
protected void Page_Load(object sender, EventArgs e)
{
txtHeaderSearch.Attributes.Add("onkeydown", "submitSearchOnEnter(event)");
}
I had a similar issue.
It worked for me when using onKeyUp:
function bodyOnKeyUp(event)
{
if (!event) {event=window.event;}
var Esc = 27;
switch (event.keyCode) {
case Esc:
document.location.href='URL';
break;
}
}
<body onKeyUp="bodyOnKeyUp(event)">
...
</body>

Resources