How to add DoubleClick Event To List Box - c#

I have a asp Listbox Control as
<asp:ListBox ID="lstBox" runat="server" CssClass="listBox"></asp:ListBox>
I want to add a doubleclick event to the Listbox I want a popup. I have googled it and haven't found any of my requirement.
Is there any event that captures the DoubleClick event. If not any alternatives to achieve this.
By the way I want to do it everything on the server side and I'm doing it in WebForm

Well use a hidden input field here to indentify when ever the listbox was clicked:
<%# 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);
}
}
</script>
<html>
<head>
<title></title>
<script lang="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 id="Form1" runat="server">
<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" />
</form>

You can add double click event on page load to listbox and can detect double click on page load as below code.
<asp:ListBox ID="lstitem" runat="server"></asp:ListBox>
page load
protected void Page_Load(object sender, EventArgs e)
{
if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "lstdbclick")
{
//your popup code here
}
lstitem.Attributes.Add("ondblclick", ClientScript.GetPostBackEventReference(lstitem, "lstdbclick"));
}

Related

Enable and disable textbox through button?(asp.net)

enter image description hereMy tutor gave me the following task: Create a form with 3 buttons(Save(Guardar) Edit(Editar) Delete(Eliminar) ).
But my real question is this:
I need to link my text box to my button allowing me to disable it and enable it at will but I can't conect them trough code.
PS:If you answer this question try as much as you can to be specific about the terms you use since I've started learning asp.net a few days ago.
enter image description here
First, you need to create the button event.
Double click to the button on your form, then the VB will create a button event automatically.
Now, you can code.
For i.e:
private void button1_Click(object sender, EventArgs e)
{
textBox1.Enabled = false;//Disable textbox1 on the form
}
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="RedioButton.aspx.cs" Inherits="Shoping_Cart.RedioButton" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Enable Disable Textbox Using Jascript Radio Button Click</title>
<script type="text/javascript">
function EnableDisableTextBox() {
var status = document.getElementById('<%=rbtEnable.ClientID %>').checked;
if (status == true) {
document.getElementById('<%=TextBox1.ClientID %>').disabled = false;
} else {
document.getElementById('<%=TextBox1.ClientID %>').disabled = true;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Width="228px"></asp:TextBox>
<br />
<br />
<asp:RadioButton ID="rbtEnable" runat="server" Text="Enable" onclick="javascript:EnableDisableTextBox();" GroupName="1" />
<asp:RadioButton ID="rbtDisable" runat="server" Text="Disable" onclick="javascript:EnableDisableTextBox();" GroupName="1" />
</div>
</form>
</body>
</html>

ASP.NET OnTextChanged method

I have ASP.NET website, there is a page when user have to enter text in textbox and image should appear depends the text that is entered. Everything is working but image appears only when you press Enter, is there a way image to appear as you entering the letters not by pressing Enter?
<asp:TextBox ID="initials" runat="server" Width="50px" OnTextChanged="initials_TextChanged" AutoPostBack="true"></asp:TextBox>
Code behind:
protected void initials_TextChanged(object sender, EventArgs e)
{
if(this.initials.Text == "A") { prvwleft.ImageUrl = "~/Images/left/A1.jpg"; }
}
In asp.net, OnTextChanged event fires when you leave the focus.
In your case, you should go for KeyDown event.
Asp.net Textbox doesn't have server side KeyDown event, so we will have to do it using jquery:
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.3.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
$('#initials').keypress(function () {
if ($(this).val() == "A") {
$('#prvwleft').ImageUrl = "~/Images/left/A1.jpg";
}
else {
$('#prvwleft').ImageUrl = "~/Images/left/A1.jpg";
}
})
});
</script>
You need to call onkeypress event in javascript like this
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function tSpeedValue(txt)
{
alert("hi");
var at = txt.value;
alert(at);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" onkeypress="tSpeedValue(this)"></asp:TextBox>
</div>
</form>
</body>
</html>
and if you want to call it in server side
<asp:TextBox ID="TextBox2" runat="server" onkeypress="__doPostBack(this.name,'OnKeyPress');" ></asp:TextBox>
and in .cs page the code should be like
protected void Page_Load(object sender, EventArgs e)
{
var ctrlName = Request.Params[Page.postEventSourceID];
var args = Request.Params[Page.postEventArgumentID];
if (ctrlName == TextBox2.UniqueID && args == "OnKeyPress")
{
TextBox2_OnKeyPress(ctrlName, args);
}
}
private void TextBox2_OnKeyPress(string ctrlName, string args)
{
//your code goes here
}

ListBox SelectedIndex always -1 in slectedindexchanged event handler

In the sample code there are two listboxes. In ListBox1 the items are defined in Default.aspx. In ListBox2 they are defined in the codefile. ListBox1 behaves as expected, the text box is updated whenever an item is selected. When ListBox2 triggers an event the selectedindex is always -1.
Default.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox1" runat="server" OnSelectedIndexChanged="lb1_OnSelectedIndexChanged" AutoPostBack="true">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
</asp:ListBox>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:ListBox ID="ListBox2" runat="server" OnSelectedIndexChanged="lb1_OnSelectedIndexChanged" AutoPostBack="true">
</asp:ListBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
Default.aspx.cs:
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<string> ab = new List<string>();
ab.Add("a");
ab.Add("b");
ListBox2.DataSource = ab;
ListBox2.DataBind();
ListBox1.SelectedIndexChanged += new EventHandler(lb1_OnSelectedIndexChanged);
ListBox2.SelectedIndexChanged += new EventHandler(lb2_OnSelectedIndexChanged);
}
protected void lb1_OnSelectedIndexChanged(object sender, EventArgs e)
{
if (ListBox1.SelectedIndex != -1)
{
TextBox1.Text = ListBox1.SelectedItem.Text;
}
}
protected void lb2_OnSelectedIndexChanged(object sender, EventArgs e)
{
if (ListBox2.SelectedIndex != -1)
{
TextBox2.Text = ListBox2.SelectedItem.Text;
}
}
}
The list of items has been moved to Default.aspx to get it working, but I'd really like to know what I'm doing wrong with this. -- Thanks!
You can't databind ListBox2 on every page load. Try:
Page_Load()...
{
if(!IsPostBack)
{
//databind listbox2
}
}
Try reading up a bit on the ASP.NET page lifecycle. On the postback, the ListBox2 will have it's correct state due to viewstate.
Your current code resets it. Hence the -1.

Access the Label value on Page load in c# when value set through jQuery

I am posting this question again, maybe this time more accurate description.
The problem is , I am using jQuery to set the Label's text value and it works fine on browser, but when I want to save it to string, it does not save it. Here is the
front End Html Code.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(window).load(function () {
var myNewName = "Ronaldo";
$('#<%= Label1.ClientID %>').text(myNewName);
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
And here is the Back End C# Code On Page Load
using System;
using System.Web.UI;
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
string mynameCheck = Label1.Text;
if (mynameCheck=="Ronaldo")
{
Response.Write("Yes Name is Fine");
}
else
{
Response.Write("Name's not Fine");
}
}
}
The result displayed is
Name's not Fine
Ronaldo
Seems like the string is still Null. Is there any problem of rendering??
label is not input type so you can not get changed values through jquery on server side. You can use hidden field for this purpose.
Your server side code (c#) can not access the form data until your client side code (HTML/Javascript) posts it.
Why do you want to the name already at the PageLoad event?
You could add a asp:Button with an attached onClick event handler to read the value of your asp:Label.
Labels do not maintain viewstate. The server will not post that information back to the server. You can try explicitly enabling the ViewState on your Label, but if that doesn't work, you will have to store that value in a hidden field.
First Call Page Load event and after that call JQuery Window.Load event.
So if you want to set any content in Label then you can do using onClientClick of button.
For ex.
<asp:Button ID="btn" runat="server" Text="Click me" OnClientClick="SetClientValues();" />
<script type="text/javascript">
function SetClientValues() {
var myNewName = "Ronaldo";
$('#<%= Label1.ClientID %>').text(myNewName);
}
</script>
At server side button event you can get Label values that sets at client side.
protected void btn_Click(object sender, EventArgs e)
{
string mynameCheck = Label1.Text;
if (mynameCheck=="Ronaldo")
{
Response.Write("Yes Name is Fine");
}
else
{
Response.Write("Name's not Fine");
}
}
It will print Yes Name is Fine
This should do it:
<script type="text/javascript">
$(window).load(function () {
if($('#<%= Txt1.ClientID %>').val() != "Ronaldo"){
var myNewName = "Ronaldo";
$('#<%= Txt1.ClientID %>').val(myNewName);
$('#<%= Label1.ClientID %>').text(myNewName);
$('#<%= Btn1.ClientID %>').click();
}
});
</script>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="Txt1" runat="server" style="display:none"></asp:Label>
<asp:Button ID="Btn1" runat="server" style="display:none" Click="Btn1_Click"></asp:Label>
</form>
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
{
Label1.Text=Txt1.Text;
string mynameCheck = Label1.Text;
if (mynameCheck=="Ronaldo")
{
Response.Write("Yes Name is Fine");
}
else
{
Response.Write("Name's not Fine");
}
}
}
protected void Btn1_Click(object sender, EventArgs e)
{ }
Hope it helps :)

Why dynamically created user controls disappear when controls are not doing full postbacks?

Based on my current understandings, when you have an UpdatePanel control there is no full postback. Therefore if I dynamically add custom user controls and they have UpdatePanels that are updated in their code they shouldnt disappear from the page they are loaded into, right? Apparently not. I made a simple project to test and still my dynamic controls disappear when clicked even though they should not be triggering a full postback. I have an aspx page that loads the controls:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="TESTmultipleScriptManagerControls.aspx.cs" Inherits="myPlayground.TESTmultipleScriptManagerControls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
With the following code behind:
protected void Button1_Click(object sender, EventArgs e)
{
TESTcontrol1 temp = LoadControl("TESTcontrol1.ascx") as TESTcontrol1;
PlaceHolder1.Controls.Add(temp);
TESTcontrol1 temp2 = LoadControl("TESTcontrol1.ascx") as TESTcontrol1;
PlaceHolder1.Controls.Add(temp2);
}
And a simple user control:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="TESTcontrol1.ascx.cs" Inherits="myPlayground.TESTcontrol1" %>
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
</asp:ScriptManagerProxy>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
With the following code behind:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = System.DateTime.Now.ToString();
}
Any ideas on why the controls are disappearing even though there shouldnt be a postback triggering?
OK based on my current understandings, when you have an UpdatePanel
control there is no full postback.
Postbacks triggered from UpdatePanels always execute the full page life-cycle. All the events are triggered normally. It makes no difference whether you use an UpdatePanel or not. Every time you add a control programmatically you need to re-add it on every postback.
Read this post, it may help you understand a bit better what's going on here.
By End of the Page Life Cycle all the controls generated at Runtime/Compile Time will be Disposed.
Below are the Page Events. Please set the BreakPoint on each Event and you can figure out that on each Asynchronous/Synchronous Request, all these Page Events are being executed.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
}
protected override void OnPreLoad(EventArgs e)
{
base.OnPreLoad(e);
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void OnLoadComplete(EventArgs e)
{
base.OnLoadComplete(e);
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
protected override void OnUnload(EventArgs e)
{
base.OnUnload(e);
}
You need to populate them on each Page Init event in order to persist Viewstate, also events for controls added inside an update panel during button clicks do not seems to get registered until the next Postback. My suggestion is to keep a list of what you have added dynamically, and store it in a session variable

Categories

Resources