I have a list of asp:LinkLabels which are contained a repeater.
I have a button that says "Get Started" when a user clicks on this button, I want it to execute the first item in the repeater.
I have this code working fine in Chrome, but not in any other browser:
<asp:Button ID="SubmitInfo" runat="server" Text="Save and Get Started" Width="218px" OnClientClick="__doPostBack('rptList$ctl01$Label15','')" CssClass="submit-button-huge" OnClick="SubmitInfo_Click" />
Is there a way to do this server side without using the __doPostBack javascript?
Is there anything glaringly wrong here?
Difficult to answer without your whole code. I'll assume your LinkLabels are LinkButtons named "lb1", and you use commandName and CommandArgument, Repeater_ItemCommand
I would try something like this in the repeater.ItemCreated
private void Repeater_ItemCreated(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
{
var lb = (LinkButton)e.Item.FindControl("lb1");
if(IsPostBack && e.Item.ItemIndex==0)
{
SubmitInfo.Click+= (source,args)=>Repeater_ItemCommand(lb,new RepeaterCommandEventArgs(e.Item,lb,new CommandEventArgs(lb.CommandName,lb.CommandArgument)));
}
...
Hope this will help
Related
I am sorry I can't solve what must be simple to everyone else even after reading so many posts. Simply stated... I have an ASP webpage I created in VS2010 and when the user clicks the 'Send Email' button, I want to change the button either using JQuery then calling my code-behind to do the actual send. Alternatively, I want the C# code behind to change the button text while it is in the process of sending so the user knows to hold on.
I am new to JQuery and still pretty new to C#. My website is www. themilwaukeehandcenter.com. If you click the contact us... this is where I want the code. I have tried
$("ContactUsButton").click(function () {
ContactUsLabel.Text = "Processing";
});
on the main page. I have also tried
protected void ContactUsButton_Click(object sender, System.EventArgs e)
{
ContactUsLabel.Text = "Sending... Please Wait";
ContactUsLabel.Refresh();
I know this should be simple but hours later.... reading so many posts later.... I feel no farther. I can't quite figure out how the JQuery onclick and the ASP onclick interact. I also don't know why C# flags the Refresh() as not valid. Any help welcome.
I actually placed a ContactUsLabel on the form too when I couldn't get the ContactUsButton to do what I intended. I am too knew to understand what you mean by 'How have you declared your button, but this is the code that creates it:
<asp:Button ID="ContactUsButton" runat="server"
OnClick="ContactUsButton_Click" Text="Send Email" Width="120px"/>
<asp:Label ID="ContactUsLabel" runat="server" Text="" Width="120px">
</asp:Label>
Your WinForm Code:
<asp:button id="ContactUsButton" runat="server" onclick="ContactUsButton_Click" cssClass="myButton" text="Contact Us" />
On submit use the jQuery script to change button text.
$(document).ready(function() {
$('.myButton').click(function(){
$(this).attr('value', 'Processing...');
});
});
View in fiddler: view
Finally at the server side:
protected void ContactUsButton_Click(object sender, System.EventArgs e)
{
ContactUsButton.Text = "Thank You";
}
I have a bunch of LinkButtons on an asp.net page and need to set the visibility property of all the other LinkButtons on the page that have the same onclick attribute. I'm looking for a server side solution.
In the click handler I've gotten as far as listing the LinkButtons on the Page recursively but am stumped at how to tell if each LinkButton I find does or doesn't have a matching click handler.
The EventHandler property doesn't seem to contain any good info...
What is the best way to approach this?
You can give your linkbuttons a custom atttribute (for simplicity it can be the same as event handler name) e.g.
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton_Click" tag="LinkButton_Click">LinkButton1</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton_Click" tag="LinkButton_Click">LinkButton2</asp:LinkButton>
Then in your server-side code you can simple compare the attributes
protected void LinkButton_Click(object sender, EventArgs e)
{
// your recursive code retrieving current linkbutton
// ...
if ((sender as LinkButton).Attributes["tag"] == currentLinkbutton.Attributes["tag"])
{
// do your magic
}
}
An ugly solution would be to give all of those buttons the same CommandName attribute. Then you can just search for all link buttons with that attribute. Otherwise I am not sure that there is a great way to track the event handler used by each LinkButton.
Did you try linkbtn.Attributes["OnClick"]
For some reason, this code isn't working:
Code-Behind
protected void btnNote_Clicked(object sender, EventArgs e)
{
DevExpress.Web.ASPxPopupControl.ASPxPopupControl notePopup = (DevExpress.Web.ASPxPopupControl.ASPxPopupControl)Master.FindControl("TaskBar").FindControl("pcNote").FindControl("notePopup");
notePopup.ShowOnPageLoad = true;
}
.aspx (event)
<asp:Button ID="btnNote" runat="server" Text="Add Note" OnClick="btnNote_Clicked" />
I need to write a function that takes this popup control ('notePopup') and displays it, and I believe this is supposed to work, but for some reason, once the page is reloaded, there is no popup.
#Jordan, try to add the ASPxPopupControl inside the same MS UpdatePanel. In this case, I believe everything will work properly. What are your results?
I figured it out myself, although what I'm doing now I'm 100% sure I already did, and I'm not sure what else could have changed that affected my results since then, so I'm closing the question.
In one of my web page, there are 12 checkbox controls in a div tag. I want to make sure the user check at least one checkbox in a div after they submit the form.in one of my asp.net web page. Any idea? I mean server side. I have done client side, but as you know, no one could guarantee all client browser enable javascript.
Since you're using ASP.Net, you may want to consider using the
<asp:CheckboxList />
control, and add an <asp:CustomValidator> plus validation functions that ensure one checkbox was checked.
While I agree with what others said about the validators, this code does what you want:
int i = 0;
foreach (Control ctl in myForm.FindControl("myDiv").Controls)
{
if (ctl is CheckBox)
{
if (((CheckBox)ctl).Checked)
i++;
}
}
Can you use JQuery? If so, check this out:
Get a list of checked checkboxes in a div using jQuery
Does it have to be is C#? Sounds a lot simplier if you just did it in javascript.
You want to look at the OnClientClick property of the asp:CheckBox control.
Here is an example:
<asp:CheckBox runat="server" OnClientClick="alert(this.checked);" />
In your javascript code (called by the handler) you can loop through all children of the div, looking for checkbox type inputs. jQuery is best for that. You could use something like this:
function countChecked() {
return $("input:checked").length;
}
That function above will return the number of checked checkboxes. After that it's trivial to validate your form. Just remember to return false from the handler called by OnClientClick to avoid a postback (in the event your form doesn't validate).
I just realized you edited your question while I was typing an answer. The above is a client side solution only.
If you just want a server side check for this and dont mind autopostback, give this a try. Just set an event handler for the SelectedIndexChanged event and check to see if an option is selected there. You can then display an error of your choice.
Here is the checkboxlist code:
<asp:CheckBoxList ID="chkBxList" runat="server" AutoPostBack="true"
onselectedindexchanged="chkBxList_SelectedIndexChanged">
<asp:ListItem>option1</asp:ListItem>
<asp:ListItem>option2</asp:ListItem>
<asp:ListItem>option3</asp:ListItem>
</asp:CheckBoxList>
<asp:Label id="lblError" runat="server"></asp:Label>
Codebehind:
protected void chkBxList_SelectedIndexChanged(object sender, EventArgs e)
{
bool oneSelected = false;
foreach (ListItem item in chkBxList.Items)
{
if (item.Selected)
oneSelected = true;
}
if (!oneSelected)
lblError.Text = "Please select an option from the checkbox list.";
else
lblError.Text = "At least one checkbox is selected";
}
Even if the client disables JS this will still make sure they choose at least one.
What I'm trying to do is basicly what this photo shows.
When I select something from the treeview it passes a parameter to a linq command that selects some data from the database. For every item in the selection I want to make a Icon and a text that represents if the item is a folder or a file.
When I push the Icon or the Link i want it to do the same as i would push the treeview, pass a parameter to a linq command that selects again from the database and populates the placeholder.
The way I'm doing this now is to make at runtima a Panel that holds the ImageButton and LinkButton. Then i add the Panel to the ContentPlaceHolder.
The problem with this that it does it every time i select something new and also i cant get it to work if the push the icon or the linkbutton, only the from the treeview.
Could i use some controller and css to get this look for the Icons ?
Is there another better way ?
This is basicly the same system as the Explorer uses in Windows, Treeview shows only the folder but the window shows the folders and files. When i click a folder that folder opens up and the main window is populated with items that are inside that folder. If i click a file a editor opens up with the contents of the file.
Not sure I understand you question 100% but I think I got the gist.
I'm assuming that you want the folders first, then the files. I would create two repeaters in this area, one to hold the Folder Image and link buttons, and the other for the file image and link buttons.
Break your linq command into two queries, one to get the folders and one for files. Then just bind the repeaters to the corresponding repeaters.
Here's a bit of code to get you started:
<asp:Repeater ID="rptFolders" runat="server" OnItemCommand="rptFolders_ItemDataBound">
<ItemTemplate>
<div>
<asp:ImageButton ID="btnImage" runat="server" />
<asp:LinkButton ID="btnLink" runat="server" />
</div>
</ItemTemplate>
</asp:Repeater>
And the code behind after calling DataBind():
protected void rptFolders_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Book book = (Book)e.Item.DataItem; //Or whatever your passing
ImageButton btnImage = e.Item.FindControl("btnImage");
LinkButton btnLink = e.Item.FindControl("btnLink");
btnLink.Text = book.Name;
btnLink.Click += new EventHandler(FolderClicked);
btnImage.Click += new ImageClickEventHandler(FolderClicked);
}
}
You can obviously do whatever you want with Click Events, just added those in for good measure.
I would probably create a Folder and File Control and use those instead of the imagebutton / linkbutton combo, this way I could store more information about the Folder / File to access them later without having to do another query to get the ID or what not. But there are a million approaches to this, pick the one you think is best.
Let me know if you need more guidance w/ this solution, or if I didn't understand your question.
Happy Coding...
Sorry had to add as another Answer. Here's a quick sample of the folder user control.
Create your Control... Format however you want.
<%# Control Language="C#" AutoEventWireup="true" CodeFile="FolderButton.ascx.cs" Inherits="FolderButton" %>
<div>
<asp:ImageButton ID="btnImage" runat="server" ImageUrl="yourfolder.jpg" />
<asp:LinkButton ID="btnTitle" runat="server" />
</div>
Add Properties and Click Event to the Code Behind (don't forget to fire the click event when your image and link buttons are clicked):
public partial class FolderButton : System.Web.UI.UserControl
{
public int DatabaseId { get; set; }
public string Name { get; set;} // you can even set your linkbutton text here.
public event EventHandler Click;
}
Create your Repeater of the FolderButton Controls:
<asp:Repeater ID="rptFolders" runat="server" OnItemDataBound="rptFolders_ItemDataBound">
<ItemTemplate>
<uc1:FolderButton ID="FolderButton1" runat="server" />
</ItemTemplate>
</asp:Repeater>
Set Folder Id on DataBinding:
protected void rptFolders_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Book book = (Book)e.Item.DataItem; //Or whatever your passing
FolderButton btnFolder = e.Item.FindControls("FolderButton1");
btnFolder.Name=book.Name;
btnFolder.DatabaseId=book.Id;
btnFolder.Click += new EventHandler(FolderClicked);
}
}
Lastly you can then do whever you want on the event Click:
void FolderClicked(object sender, EventArgs e)
{
int id = ((FolderButton)sender).DatabaseId;
/// Do something with your Id
}
Let me know if anything is unclear. This is just a quick freehand sample, so forgive any typos or bad practices... code is just for demostration purposes only.