How to Bind and Insert in Repeater control in Asp.net? - c#

I have created the table cm_table and cm_table_copy on MySQL database.
On cm_table I have recorded three rows, instead the cm_table_copy is empty
After that added new web form to website.
Drag and drop a Repeater control from Toolbox>Data to .aspx page.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Repeater.aspx.cs" Inherits="Repeater" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="repcomment" runat="server">
<ItemTemplate>
<fieldset>
<legend style="font-size: 12px; font-weight: bold; color: Red; margin-left: 10px;">
Repeater control in Asp.net
</legend>
<div class="pure-g">
<div class="pure-u-1 pure-u-md-1-3">
Comment
<asp:TextBox ID="txComment" runat="server" Text='<%#Eval("Comment").ToString()%>'>
</asp:TextBox>
</div>
</div>
<br />
<div class="container">
<asp:ImageButton ID="btn" runat="server" ValidationGroup="Validation2"
OnClick="btn_Click"
ImageUrl="/Images/edit_button.gif"
OnClientClick="if (!confirm('Confirm?')) return false;"
CausesValidation="true" />
</div>
</fieldset>
<br />
</ItemTemplate>
</asp:Repeater>
</div>
<asp:ValidationSummary ID="ValidationSummary1"
ValidationGroup="Validation2" runat="server"
ShowSummary="false"
ShowMessageBox="true"
CssClass="validation-summary-errors" />
</form>
</body>
</html>
I should see on the web browser the three rows recorded on cm_table and when the comment on cm_table is edited I need register the new comment on cm_table_copy
But I have this error on first access to aspx page
CS0103: The name 'txComment' does not exist in the current context
How to do resolve this?
My code-behind below
Can you help me, please?
Thank you in advance for any help
using System;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web.UI;
public partial class Repeater : Page
{
protected void btn_Click(object sender, ImageClickEventArgs e)
{
string sql = String.Format(" INSERT INTO cm_table_copy (Comment, LatestComment) ");
sql += String.Format(" VALUES(?, CURRENT_TIMESTAMP()); ");
using (OdbcConnection myConnectionString =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ca"].ConnectionString))
{
using (OdbcCommand command =
new OdbcCommand(sql, myConnectionString))
{
try
{
command.Parameters.AddWithValue("param1", txComment.Text.Replace("'", "`").ToString().ToUpper()); //<<<Line of error
command.Connection.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
command.Connection.Close();
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string sql = #String.Format(" SELECT * FROM cm_table; ");
using (OdbcConnection myConnectionString =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ca"].ConnectionString))
{
using (OdbcCommand command =
new OdbcCommand(sql, myConnectionString))
{
try
{
command.Connection.Open();
DataSet ds = new DataSet();
OdbcDataAdapter adp = new OdbcDataAdapter(command);
adp.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
repcomment.DataSource = ds;
repcomment.DataBind();
}
}
catch (Exception ex)
{
throw new ApplicationException("operation failed!", ex);
}
finally
{
command.Connection.Close();
}
}
}
}
}
}
Update #01
I have tried this but the FindControl always returns null
protected void btn_Click(object sender, ImageClickEventArgs e)
{
TextBox txComment = (TextBox)repcomment.FindControl("txComment");
}

Related

Asp.net controls are not accessible in code behind. Getting error 'The name 'txtUserEmail' does not exist in the current context'

I don't know where I am making a mistake. whenever I am trying to access textbox in cs file, getting error 'The name 'txtUserEmail' does not exist in the current context'
tempSendEmail.aspx.cs
namespace MEPEmailServiceClient
{
public partial class tempSendEmail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSignUp_Click(object sender, EventArgs e)
{
try
{
MepClient c = new MepClient();
var con = new SqlConnection("Data Source=LAPTOP-U8N2419N;Initial Catalog=Employee;Integrated Security=True");
var cmd = new SqlCommand("Insert into [empDetail] values(#UserEmail)", con);
cmd.Parameters.AddWithValue("#UserEmail",txtUserEmail.Text.ToString());
con.Open();
cmd.ExecuteNonQuery();
con.Close();
c.sendMail(txtUserEmail.Text.ToString());
}
catch(FaultException fe)
{
Response.Write(fe.Message);
}
}
}
}
tempSendEmail.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="tempSendEmail.aspx.cs" Inherits="MEPEmailServiceClient.tempSendEmail" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="txtUserEmail" />
<br />
<asp:Button ID="btnSignUp" runat="server" OnClick="btnSignUp_Click" Text="SignUp" />
<br />
<br />
EmailLog.aspx<br />
</div>
</form>
</body>
</html>

Style to display data in gridview

I am trying to display data in Gridview in c#. I have been asked to make the gridview look as close as a table in SQL database. I have been able to achieve a bit but my grid is far from a Sql table. Can someone help me out? This is what I tried so far.
Aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Reports.aspx.cs" Inherits="Reports" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Enter Query"></asp:Label> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
<p>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
<div id="grdCharges" runat="server" style="width: 875px; overflow: auto; height: 600px;">
<asp:GridView ID="grd1" runat="server" AutoGenerateColumns="true" Width="150%">
</asp:GridView>
</div>
</p>
</form>
</body>
</html>
C#
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Reports : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = null;
SqlDataReader reader = null;
try
{
string q = TextBox1.Text.ToString();
string strcon = "Data Source=***********";
con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = q;
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.
var dataTable = new DataTable();
dataTable.Load(reader);
grd1.DataSource = dataTable;
grd1.DataBind();
}
catch (Exception ex)
{
//....
}
finally
{
if(reader != null)
reader.Close();
if(con != null && con.ConnectionState != ConnectionState.Closed)
con.Close();
}
}
}
This is how my table looks like -
Current table -
Current table
Target table -
Targettable

Browser is giving "200 stream not found netstream.play.streamnotfound clip" error what is the error in the code

i have write a code to fetch the video from the server folder and play it on web page. i am using flowplayer.it shows the video player with controls but not playing the video.what could be the problem . code is attached
Code in aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="WebsiteTaskAsp.index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="FlowPlayer/flowplayer-3.2.12.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div style="height: 73px">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Label ID="lblFilename" runat="server" Text="File Name :"></asp:Label>
<asp:Label ID="lblUploadMsg" runat="server"></asp:Label>
<asp:TextBox ID="videoNameTextbox" runat="server"></asp:TextBox>
<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" /><br /><br />
</div>
<div>
<hr />
<asp:DataList ID="DataList1" Visible="true" runat="server" AutoGenerateColumns="false"
RepeatColumns="2" CellSpacing="5">
<ItemTemplate>
<u>
<%# Eval("vname") %></u>
<hr />
<a class="player" style="height: 300px; width: 300px; display: block" href='<%# Eval("vpath") %>'>
</a>
</ItemTemplate>
</asp:DataList>
<script src="FlowPlayer/flowplayer-3.2.12.min.js" type="text/javascript"></script>
<script type="text/javascript">
flowplayer("a.player", "FlowPlayer/flowplayer-3.2.16.swf", {
plugins: {
pseudo: { url: "FlowPlayer/flowplayer.pseudostreaming-3.2.12.swf" }
},
clip: { provider: 'pseudo', autoPlay: false },
});
</script>
</div>
</form>
</body>
</html>
**Code in aspx.cs**
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
namespace WebsiteTaskAsp
{
public partial class index : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=Khawaja\\SQLEXPRESS;Initial Catalog=TaskDB;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
using (SqlConnection con = new SqlConnection("Data Source=Khawaja\\SQLEXPRESS;Initial Catalog=TaskDB;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select * from videos";
cmd.Connection = con;
con.Open();
DataList1.DataSource = cmd.ExecuteReader();
DataList1.DataBind();
con.Close();
}
}
}
protected void btnUpload_Click(object sender, EventArgs e)
{
if(FileUpload1.HasFile)
{
string str = FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(Server.MapPath(".") + "//UploadedVideos//"+str);
string path = "~//UploadedVideos//"+str.ToString();
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO videos VALUES('"+videoNameTextbox.Text+"','"+path+"')",con);
cmd.ExecuteNonQuery();
con.Close();
lblUploadMsg.Text = "Video Uploaded Successfully";
}
}
}
}

ASP.NET C# The Wait Operation Timed Out

Getting an error that has crashed my entire site. The wait operation is timing out, i have read the documentation on using{} blocks but cannot understand why this is happening. Any help is appreciated.
[Win32Exception (0x80004005): The wait operation timed out]
The page i have been working on most recently is
itemediting.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="itemediting.aspx.cs" Inherits="admin_itemediting" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>elmtree - Admin</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" />
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="../styles/mylist.css" />
</head>
<body>
<form id="form1" runat="server">
<img src="images/ELleft.png" style="width:226px; height:52px; margin-top: 3px; margin-left: 17px; text-align: justify; float: none;"/></a></li>
<div class="container">
<h1> Item Edit </h1> </div>
<div class="container">
<div class="form-group">
<label class="col-sm-2 control-label">Item name: </label>
<div class="col-md-4">
<asp:TextBox ID="itemnametext" runat="server" Text="" CssClass="form-control">
</asp:TextBox>
</div>
<div class="pull-right">
<asp:Button CssClass="btn btn-primary btn-lg" ID="updatebutton" role="button" runat="server" Text="save" OnClick="updatebutton_Click" />
</div>
</div>
</div>
</form>
</body>
</html>
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class admin_itemediting : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int row = 0;
if (Request.QueryString["itemID"] != null)
{
row = int.Parse(Request.QueryString["itemID"]);
}
else
{
Response.Redirect("itemedit.aspx");
}
}
string connectionString = WebConfigurationManager.ConnectionStrings
["ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionString);
myConnection.Open();
string query = "SELECT * FROM reports WHERE ID=#rowid";
SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.Parameters.AddWithValue("#rowid", row);
SqlDataReader rdr = myCommand.ExecuteReader();
while (rdr.Read())
{
string myname = rdr["itemname"].ToString();
itemnametext.Text = myname;
}
}
protected void updatebutton_Click(object sender, EventArgs e){
string connectionString = WebConfigurationManager.ConnectionStrings ["ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionString);
myConnection.Open();
string itemnametextupdate = itemnametext.Text;
string query = "UPDATE reports SET itemname = #itemnewname";
SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.Parameters.AddWithValue("#itemnewname", itemnametextupdate);
myCommand.ExecuteNonQuery();
myConnection.Close();
Response.Redirect("updateimage.aspx");
}
public object row { get; set; }
}
Just a suggestion.
if (!IsPostBack)
{
int row = 0;
if (Request.QueryString["itemID"] != null)
{
row = int.Parse(Request.QueryString["itemID"]);
}
else
{
// here even if you make redirect the code continue to run
// and you do not know whats going on then... a call to db and a redirect.
Response.Redirect("itemedit.aspx");
// so here add a return;
return;
}
}
Next possible bug is that on post back the row is not saved on viewstate and take a default value. Make your row variable as:
int cRow
{
set
{
ViewState["cRow"] = value;
}
get
{
if (ViewState["cRow"] != null)
return Convert.ToInt32(ViewState["cRow"]);
else
return -1;
}
}

Asp.net Dropdownlist selected index changed AND TextChanged Events not Fire? (C#)

I am new to ASP>NET(C#). But I am using Winform before.
In my Project I have two dropdown list. if i change the one,.. the second will change auto.
But even event also not firing in ASP.net. Without event how to i manage that?.
<div id="Div1" class="ui-content ui-body-a" runat="server">
<asp:dropdownlist id="ddlOutlet" runat="server" autopostback="True" onselectedindexchanged="ddlOutlet_SelectedIndexChanged"
ontextchanged="ddlOutlet_TextChanged">
</asp:dropdownlist>
<br />
<asp:dropdownlist id="ddlServedAt" runat="server">
</asp:dropdownlist>
<br />
<asp:button id="btnLogin" runat="server" text="LogIn" />
</div>
C#
I already Added items in both dropdown list in page load event. But When I change the ddlOutlet selected index changed Event not firing. So I tried to TextChanged Events Also. But nothing happened. What is problem?.
Page Load Event -
protected void Page_Load(object sender, EventArgs e)
{
HelpingFunctions hp = new HelpingFunctions();
string id = Request.QueryString["id"];
MySqlConnection connection = new MySqlConnection("server=192.168.1.100;username=mcubic;password=mcs#2011$;database=mcubic;");
string query = "SELECT c.Outlet_Master_Name,d.Fbserved_Served FROM mcs_user a, mcs_user_outlet b,outlet_master c,fb_served d WHERE a.Mcs_User_Id='" + id + "' and"
+ " a.Mcs_User_Id = b.Mcs_User_Outlet_User_Id and c.Outlet_Master_Id = b.Mcs_User_Outlet_Outlet_Id and c.Outlet_Master_Id=d.Fbserved_outletid";
MySqlCommand command = new MySqlCommand(query, connection);
connection.Open();
MySqlDataReader Reader = command.ExecuteReader();
while (Reader.Read())
{
ddlOutlet.Items.Add(Reader[0].ToString());
ddlServedAt.Items.Add(Reader[1].ToString());
}
connection.Close();
}
Selected Item -
protected void ddlOutlet_SelectedIndexChanged(object sender, EventArgs e)
{
MySqlConnection connection = new MySqlConnection("server=192.168.1.100;username=mcubic;password=mcs#2011$;database=mcubic;");
string query = "select Fbserved_Served from fb_served where Fbserved_outletid = (select Outlet_Master_Id from outlet_master where Outlet_Master_Name ='" + ddlOutlet.SelectedItem.ToString() + "')";
MySqlCommand command = new MySqlCommand(query, connection);
connection.Open();
MySqlDataReader Reader = command.ExecuteReader();
while (Reader.Read())
{
ddlServedAt.SelectedItem.Value = Reader[0].ToString();
}
connection.Close();
}
I read some same issue problem solutions, but they told to set AutoPostBack="True". I checked that also.
I don't know why ASP.net hard to Event firing also?.
UPDATED QUESTION
In my Project,.. I am using jquerymobile(http://jquerymobile.com/) with ASP.Net and mysql.
I have two drop down list controls and ONE button.
But When I change the ddlOutlet selected index changed Event not firing. After the button Click the Events Are fired Correctly. But Before that They not fire. I do't know Why its happen.
I Give my Complete Code Below.
My Complete ASPX File :-
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="OutLet.aspx.cs" Inherits="MobileApp.OutLet" %>
<!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>
<meta name="viewport" content="width=device-width; height=device-height; initial-scale=1.0; maximum-scale=1.5; user-scalable=no;" />
<link href="Styles/jquery.mobile-1.0b3.css" rel="stylesheet" type="text/css" />
<script src="Scripts/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.mobile-1.0b3.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server" data-ajax="false">
<div id="mainheader" class="ui-header-fixed ui-bar-a">
</div>
<div id="Div1" class="ui-content ui-body-a" runat="server">
<%-- <asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>--%>
<asp:DropDownList ID="ddlOutlet" runat="server"
AutoPostBack="True" onselectedindexchanged="ddlOutlet_SelectedIndexChanged"
ontextchanged="ddlOutlet_TextChanged">
</asp:DropDownList>
<br />
<asp:DropDownList ID="ddlServedAt" runat="server" AutoPostBack="True">
</asp:DropDownList>
<br />
<asp:Button ID="btnLogin" runat="server" Text="LogIn"
onclick="btnLogin_Click" />
<%-- </ContentTemplate>
</asp:UpdatePanel> --%>
</div>
</form>
</body>
</html>
my Code -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
namespace MobileApp
{
public partial class OutLet : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HelpingFunctions hp = new HelpingFunctions();
string id = Request.QueryString["id"];
if (!Page.IsPostBack)
{
MySqlConnection connection = new MySqlConnection("server=192.168.1.100;username=mcubic;password=mcs#2011$;database=mcubic;");
string query = "SELECT c.Outlet_Master_Name,d.Fbserved_Served FROM mcs_user a, mcs_user_outlet b,outlet_master c,fb_served d WHERE a.Mcs_User_Id='" + id + "' and"
+ " a.Mcs_User_Id = b.Mcs_User_Outlet_User_Id and c.Outlet_Master_Id = b.Mcs_User_Outlet_Outlet_Id and c.Outlet_Master_Id=d.Fbserved_outletid";
MySqlCommand command = new MySqlCommand(query, connection);
connection.Open();
MySqlDataReader Reader = command.ExecuteReader();
while (Reader.Read())
{
ddlOutlet.Items.Add(Reader[0].ToString());
ddlServedAt.Items.Add(Reader[1].ToString());
}
connection.Close();
}
}
protected void ddlOutlet_SelectedIndexChanged(object sender, EventArgs e)
{
MySqlConnection connection = new MySqlConnection("server=192.168.1.100;username=mcubic;password=mcs#2011$;database=mcubic;");
string query = "select Fbserved_Served from fb_served where Fbserved_outletid = (select Outlet_Master_Id from outlet_master where Outlet_Master_Name ='" + ddlOutlet.SelectedItem.ToString() + "')";
MySqlCommand command = new MySqlCommand(query, connection);
connection.Open();
MySqlDataReader Reader = command.ExecuteReader();
while (Reader.Read())
{
ddlServedAt.SelectedValue = Reader[0].ToString();
}
connection.Close();
}
protected void ddlOutlet_TextChanged(object sender, EventArgs e)
{
//MySqlConnection connection = new MySqlConnection("server=192.168.1.100;username=mcubic;password=mcs#2011$;database=mcubic;");
//string query = "select Fbserved_Served from fb_served where Fbserved_outletid = (select Outlet_Master_Id from outlet_master where Outlet_Master_Name ='" + ddlOutlet.SelectedItem.ToString() + "')";
//MySqlCommand command = new MySqlCommand(query, connection);
//connection.Open();
//MySqlDataReader Reader = command.ExecuteReader();
//while (Reader.Read())
//{
// ddlServedAt.SelectedItem.Value = Reader[0].ToString();
//}
//connection.Close();
}
protected void btnLogin_Click(object sender, EventArgs e)
{
}
}
}
Create the drop down list(the one that changes the content of the other one)
Activity: <asp:DropDownList ID="cmbActivity" runat="server" OnSelectedIndexChanged="cmbActivity_SelectedIndexChanged" AutoPostBack="true" />
Then in your C#, your load event should be like this
if (Session["staffId"] == null)
{
Response.Redirect("~/login.aspx");
}
else
{
if (!IsPostBack)
{
cmbActivity.DataSource = a;
cmbActivity.DataTextField = "activityName";
cmbActivity.DataValueField = "activiyId";
}
}
Your selectedIndex_Changed method still remains the same. This should work. Have more than one item in the list to be sure.
I hope this helps
try by Adding Page.IsPostBack in your page_Load event...
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//your page load code.....
}
}
EDIT - 1
Sample Code of ASPX page in Web Application
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!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:DropDownList id="id1" runat="server" AutoPostBack="true"
onselectedindexchanged="id1_SelectedIndexChanged">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList id="id2" runat="server"></asp:DropDownList>
</div>
</form>
</body>
</html>
Sample code of code behind in Web Application
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void id1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
Sample code of ASPX page in Website
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!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:DropDownList id="id1" runat="server" AutoPostBack="true"
onselectedindexchanged="id1_SelectedIndexChanged">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList id="id2" runat="server"></asp:DropDownList>
</div>
</form>
</body>
</html>
Sample code of Code behind in Website
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void id1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
OP already had this in his question, but I was missing autopostback="True"

Categories