How to add Double Click mouse event to listbox? - asp.net

I would like to add a double click mouse event to a listbox. When I double click an item I'd like to get the specific item and assign a method.
I have been searching for tuturials in this field, but tried but somehow wasn't working.
Thank you for helping !

This code works for me
protected void Page_Load(object sender, EventArgs e)
{
if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "event 1")
{
// code for the event
}
ListBox1.Attributes.Add("ondblclick", ClientScript.GetPostBackEventReference(ListBox1, "event 1"));
}

<%# Page Language="C#" %>
<script runat="server">
void Page_Load(Object sender, EventArgs e){
if(Request.Params["ListBox1Hidden"] != null
&& (string)Request.Params["ListBox1Hidden"] == "doubleclicked" {
//This means It was double click
Response.Write("Double Click was fired selected item is "
+ ListBox1.SelectedItem.Text);
}
}
void Button1_Click(object sender, EventArgs e) {
Response.Write("Button was clicked");
}
</script>
<html>
<head>
<script language="javascript">
function ListBox1_DoubleClick() {
/* we will change value of this hidden field so
that in
page load event we can identify event.
*/
document.forms[0].ListBox1Hidden.value = "doubleclicked";
document.forms[0].submit();
}
</script>
</head>
<body>
<form runat="server">
<div>Double click on Listbox
<br />
<asp:ListBox id="ListBox1"
ondblclick="ListBox1_DoubleClick()" runat="server">
<asp:ListItem Value="1">One</asp:ListItem>
<asp:ListItem Value="2">Two</asp:ListItem>
<asp:ListItem Value="3">Three</asp:ListItem>
<asp:ListItem Value="4">Four</asp:ListItem>
</asp:ListBox>
<input type="hidden" name="ListBox1Hidden" />
</div>
<div>click on button
<br />
<asp:Button id="Button1" onclick="Button1_Click"
runat="server" Text="Button"/>
</div>
</form>
</body>
</html>

Simple sample to send selected item on a listbox:
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
string test = listBox1.SelectedItem.ToString();
}

Related

ASP.NET Dropdownlist SelectedIndexChanged fired again after rasing exception

Here's my sample code.
My scenarios is
1. Change dropdownlist item it will fire SelectedIndexChanged event.
2. During SelectedIndexChanged event, raise the exception.
3. Popup error alert message
4. Click the button
5. SelectedIndexChanged fired again.
I'm really curious to know what's going on this procedure?
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(PageRequestManager_OnEndRequest);
function PageRequestManager_OnEndRequest(sender, args) {
//debugger;
var alertMessage = "Application exception has been occurred.\n\nMessage: ";
if (args.get_error() && args.get_error().httpStatusCode == '500') {
var errorMessage = args.get_error().message
var findIndex = 0;
args.set_errorHandled(true);
if ((findIndex = errorMessage.indexOf(":")) > -1) {
errorMessage = errorMessage.substr(findIndex + 2);
}
alert(alertMessage + errorMessage);
}
};
</script>
<div>
<asp:UpdatePanel runat="Server" UpdateMode="Conditional" ID="UpdatePanel1">
<ContentTemplate>
<asp:DropDownList ID="DropDownListState" runat="server" OnSelectedIndexChanged="DropDownListState_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="Michigan" Value="07"></asp:ListItem>
<asp:ListItem Text="Mississippi" Value="08"></asp:ListItem>
<asp:ListItem Text="Nevada" Value="09"></asp:ListItem>
</asp:DropDownList>
<asp:Button runat="server" ID="Button" OnClick="Button_Click" Text="Click Here" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
codebehind:
protected void DropDownListState_SelectedIndexChanged(object sender, EventArgs e)
{
int stateNo = 0;
string stateName = "California";
stateNo = Convert.ToInt32(stateName);
}
protected void Button_Click(object sender, EventArgs e)
{
string ddd = "";
}
It happens because error in event DropDownListState_SelectedIndexChanged ends the request to the server in abnormal way. That's why not all properties and states are properly propagated. So when you hit the button it will think that dropdownlist value on the server is different that the one provided by the browser and will fire DropDownListState_SelectedIndexChanged again. If you comment out the error all will work as expected.

Cannot update the data in ASPxGridControl

I found that I cannot update the data in my second grid when I clicked on the first grid control.
There are two grids in my program and there is overloaded function GetDataTable2() to get DataTable according to the focused row in grid1.
However, I dun know why the grid2 cannot be updated.
Please help!
protected void Page_Load(object sender, EventArgs e)
{
gv1.DataSource = GetDataTable1();
gv1.KeyFieldName = "ID";
gv1.DataBind();
gv2.DataSource = GetDataTable2();
gv2.DataBind();
}
protected void gv1_FocusedRowChanged(object sender, EventArgs e)
{
gv2.DataSource = GetDataTable2((int)gv1.GetRowValues(gv1.FocusedRowIndex, "ID"));
gv2.DataBind();
}
And the asp.net:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div>
<dx:ASPxGridView ID="gv1" runat="server" SettingsBehavior-AllowFocusedRow="true" SettingsBehavior-ProcessFocusedRowChangedOnServer="true" OnFocusedRowChanged="gv1_FocusedRowChanged" >
</div>
<div>
<dx:ASPxGridView ID="gv2" runat="server" >
</div>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
I think that if you set the ASPxGridView's EnableCallbacks property to false, your code will start working properly. This is the recommended way of using ASPxGridView when it is located within the MS UpdatePanel.
You don't need the UpdatePanel.
In the first grid, use the "RowClick" client side event and perform a callback in the second grid.
ClientSide Event
function gv1_RowClick(s, e) {
gv2.PerformCallback("UPDATE_SECOND_GRID|" + gv1.GetRowKey(e.visibleIndex));
}
ServerSide Event
protected void gv2_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
{
string[] args = e.Parameters.Split('|');
if (args == null || args.Length == 0)
return;
if (args[0].Equals("UPDATE_SECOND_GRID")) {
//your code with args[1] (key of the first grid)
gv2.DataSource = GetDataTable2();
gv2.DataBind();
}
}

ViewState on programmatically loaded usercontrols (restored after Page_Load)

I'm trying to create a simple web page with a navigation bar and some usercontrols (ascx) programmatically loaded.
All controls are inside an update panel.
When I click on a link button (from the navigation bar) I do the following things:
I save the current usercontrol using viewstate.
Than I reload the current usercontrol.
My 'page_load' always reloads the current control.
Always assigning the same ID to the programmatically loaded control allows me to save the usercontrol viewstate.
So everything look good except one little thing: the usercontrol viewstate in not available during the usercontrol Page_Load!
Look below for (* HERE).
The 'txtTest.Text' value is always "0" (also during postback).
It seems that the user control viewstate is restored after the (usercontrol) Page_Load.
How is it possible?
--- "DEFAULT.ASPX": ---
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="sm" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="pnlMain" runat="server">
<ContentTemplate>
<div class="links">
<asp:LinkButton ID="lnkButton1" runat="server" OnClick="lnkButton1_Click" Text="Link 1"></asp:LinkButton>
<asp:LinkButton ID="lnkButton2" runat="server" OnClick="lnkButton2_Click" Text="Link 2"></asp:LinkButton>
</div>
<br />
<asp:Panel ID="pnlCtrl" runat="server"></asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
--- "DEFAULT.ASPX.CS": ---
private string CtrlAscx
{
get
{
if (ViewState["CtrlAscx"] == null)
{
ViewState["CtrlAscx"] = String.Empty;
}
return ViewState["CtrlAscx"].ToString();
}
set
{
ViewState["CtrlAscx"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
loadMyControl();
}
private void loadMyControl()
{
if (!String.IsNullOrEmpty(CtrlAscx))
{
pnlCtrl.Controls.Clear();
Control c = LoadControl(CtrlAscx);
c.ID = CtrlAscx + "ID"; // this line is mandatory in order to mantain the usercontrol viewstate
pnlCtrl.Controls.Add(c);
}
}
protected void lnkButton1_Click(Object sender, EventArgs e)
{
CtrlAscx = "Control1.ascx";
loadMyControl();
}
protected void lnkButton2_Click(Object sender, EventArgs e)
{
CtrlAscx = "Control2.ascx";
loadMyControl();
}
-- "CONTROL1.ASCX" --
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Control1.ascx.cs" Inherits="WebTest.Control1" %>
Control1: <asp:TextBox id="txtTest" runat="server" Text="0"></asp:TextBox>
<asp:Button ID="btnTest" runat="server" />
-- "CONTROL1.ASCX.CS" --
public partial class Control1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (txtTest.Text == "0") // * HERE
{
txtTest.Text = "1";
}
}
}
Try the EnableViewState="true" attribure on txtTest as well as on your custom user control when creating it :
.
.
c.EnableViewState = true;
pnlCtrl.Controls.Add(c);

Howto add postbackurl equivalent property to <div runat="server" />

I need a div element to cause postback to the server. I can use window.location tough but then I think I won't be able to fire any event back on the server. How can I call the asp.net generated dopostback function properly? Thx in advance
The idea is that I place a linkButton, and then I use it from the Div
<form id="form1" runat="server">
<div>
<div runat="server" ID="txtTest" style="cursor:pointer;" >Click the div</div>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click" style="display:none;"></asp:LinkButton>
<br /><asp:Literal runat="server" ID="txtDebug"></asp:Literal>
</div>
</form>
and code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtTest.Attributes.Add("onclick", "__doPostBack('"+LinkButton1.ClientID+"','')");
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
txtDebug.Text = "click on div";
}

Loop in code block on click argument

Basically what i am trying to do is display a list of categories. And if the admin is logged in
i want to show some buttons next to each category. For example a button to delete it. The problem is that i dont know how to pass a parameter to the function that does the action.
Like i specify that on button click the function 'DeleteCat' must be called but if i cant pass the ID of the category to be deleted this wont work.
I know this can be done with commands and a repeater, but its not an option, i cant use a repeater.
So apparanly this is what i am aiming for:
But of course it does not work.
<%For Each Cat In Category.Children%>
<p class="SubCategory">
<%=Cat.Name%>
<%If User.Identity.Name = "Admin" Then%>
<asp:LinkButton ID="LinkButton6" runat="server" OnClick="AddItem" Text="A+" CommandArgument=<%=Cat.ID %> />
<%End If%>
</p>
<%Next %>
Your event handler AddItem should be able to evaluate the sender of the event and the CommandEventArgs
void AddItem(Object sender, EventArgs e)
{
int result;
bool returnValue;
Button clickedButton = (Button)sender;
returnValue = Int32.TryParse(clickedButton.CommandArgument, result);
// then other stuff happens ...
}
See detailed example here.
edit
Completed code sample as suggested by Brandon.
Alternative solution, using a CheckBoxList
...
<script language="C#" runat="server">
void Page_Load(Object Sender, EventArgs e)
{
if (!IsPostBack)
{
// bind data to controls
this.itemRepeater.DataSource = Category.Children;
this.adminList.DataSource = Category.Children;
this.itemRepeater.DataBind();
this.adminList.DataBind();
}
// set visibility according to user
this.itemRepeater.Visible = (User.Identity.Name != "Admin");
this.adminList.Visible = (User.Identity.Name == "Admin");
this.adminButton.Visible = (User.Identity.Name == "Admin");
}
protected void AddItem(object sender, EventArgs e)
{
foreach(ListItem currentitem in this.adminList.Items)
{
if(currentitem.Selected)
{
// do something with the selected value
int i;
bool isparsed = Int32.TryParse(currentitem.value, i);
}
}
}
</script>
</head>
<body>
<form id="mainForm" runat="server">
<asp:Repeater ID="itemRepeater" runat="server">
<ItemTemplate>
<p><%# DataBinder.Eval(Container.DataItem, "value") %></p>
</ItemTemplate>
</asp:Repeater>
<asp:CheckBoxList ID="adminList" runat="server" />
<br />
<asp:Button ID="adminButton" OnClick="AddItem" runat="server" Text="Add seleted Items" />
</form>
</body>

Resources