SQL Database not working with ASP.NET web form - asp.net

I am designing a web form on Visual Studio 2010 using ASP.NET framework. I am using SQL Database to connect my web form to the server, but there are some errors which I can't resolve?
Any help would be greatly appreciated.
I am trying to run a web form that connects to the database which when data entered into the form, stores the information into the server for the user to login later
The following is the code for the web form.
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;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=#"C:\Users\Kapalmeet Singh\Desktop\WebSite1\App_Data\Database.mdf";Integrated Security=True;User Instance=True")
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into Table1 values('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox4.Text+"')";
cmd.ExecuteNonQuery();
con.Close();
}
}
And here's the code for the web form
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table class="style1">
<tr>
<td>
First Name</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Last Name</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
User Name</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Password</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
</tr>
</table>
<div>
</div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
style="margin-left: 192px" Text="Submit" />
</form>
</body>
</html>

If your creating a registration it would be like this. Change the names for these the way they are called in your database and for the other ones to. (#FirstName, #Surname, #Password, #Username,)
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["YOUR_CONNECION_STRING_HERE"].ConnectionString);
conn.Open();
string insertQuery = "insert into Table1(FirstName, Surname, Username, Password) " +
"values(#FirstName, #Surname, #Password, #Username,)";
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.AddWithValue("#FirstName", Textbox1.Text);
com.Parameters.AddWithValue("#Surname", Textbox2.Text);
com.Parameters.AddWithValue("#Password", Textbox3.Text);
com.Parameters.AddWithValue("#Username", Textbox4.Text);
//This actually executes the query with the given values above.
com.ExecuteNonQuery();
conn.Close();
Response.Redirect("Name_Of_Any_Page_Thats_On_Your_Site.aspx");
Response.Write("there was a problem with your registration");
}
catch (Exception problem)
{
//throw Exception ;
Response.Write("Error Message: " + problem.ToString());
throw;
}

Related

Why do i get Exception User-unhandled System.Data.sqlException: 'Conversion failed when converting the nvarchar value '400,000' to datatype int.'

I am developing a business app using ASP.NET and C#, when I input data and click the save button, it throws that error:
Exception User-unhandled
System.Data.sqlException: 'Conversion failed when converting the nvarchar value '400,000' to datatype int.'
<%# Page Title="" Language="C#" MasterPageFile="~/SMS.Master" AutoEventWireup="true" CodeBehind="CoursePage.aspx.cs" Inherits="Student_Management_System.CoursePage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<h1>Course Page</h1>
<div style ="background-color:aqua; width: 292px; margin-left: 0px;">
<table>
<tr>
<td>Course Name:</td>
<td>
<asp:TextBox ID="TxtCourseName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Course Fee:</td>
<td>
<asp:TextBox ID="TxtCourseFee" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Course Duration:</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="-- SELECT --" Value="select"
Selected="True"></asp:ListItem>
<asp:ListItem Text="3 Months" Value="3 Months"></asp:ListItem>
<asp:ListItem Text="6 Months" Value="6 Months"></asp:ListItem>
<asp:ListItem Text="9 Months" Value="9 Months"></asp:ListItem>
<asp:ListItem Text="1 Year" Value="1 Year"></asp:ListItem>
<asp:ListItem Text="2 Years" Value="2 Years"></asp:ListItem>
</asp:DropDownList></td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="ButCourse" runat="server" Text="Save"
OnClick="ButCourse_Click" /></td>
<td>
</tr>
</table>
</div>
</asp:Content>
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Student_Management_System
{
public partial class CoursePage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ButCourse_Click(object sender, EventArgs e)
{
string mainconn = ConfigurationManager.ConnectionStrings["Myconnection"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(mainconn);
string sqlquery = "Insert into [dbo].[Course] (CourseName, CourseFee, CourseDuration) values (#CourseName, #CourseFee, #CourseDuration)";
SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
sqlconn.Open();
sqlcomm.Parameters.AddWithValue("#CourseName", TxtCourseName.Text);
sqlcomm.Parameters.AddWithValue("#CourseFee", TxtCourseFee.Text);
sqlcomm.Parameters.AddWithValue("#CourseDuration", DropDownList1.Text);
sqlcomm.ExecuteNonQuery();
Response.Write("<script>alert('Course Added Successfully.!!');</script>");
TxtCourseName.Text = "";
TxtCourseFee.Text = "";
sqlconn.Close();
}
}
}

Save texbox text to SQL Server in ASP.NET website

I have a website in ASP.NET and I have a textbox that the user type in text. I want to save this into a SQL Server database that saves all other data from site. I have tried several ways and got to the code below. It throws up the error below
Any help as I'm lost on this now....
Error:
The name 'txtName' does not exist in the current context Step9.aspx.cs 67
Page markup:
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Step9.aspx.cs" Inherits="Step1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div class="question" id="question">
<table border="1" style="border-collapse: collapse">
<tr>
<td style="width: 150px">
Name:<br />
<asp:TextBox ID="txtName" runat="server" Width="140" />
</td>
<td style="width: 100px">
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Insert" />
</td>
</tr>
</table>
</div>
</asp:Content>
C# codebehind:
protected void Insert(object sender, EventArgs e)
{
string name = txtName.Text;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO learners (stickTwistBefore) VALUES (#stickTwistBefore)"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Parameters.AddWithValue("#stickTwistBefore", name);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
// this.BindGrid();
}
I noticed that you have specified Step1 class name as Inherits attribute. Ensure that it matches the class name in your Step9.aspx.cs file. I guess Step9 but just confirm.
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Step9.aspx.cs" Inherits="Step1" %>

how to add null values in dropdownlist to sql server by using asp.net

I have created two table in SQL server they are:
TblStudent:
TblProvince:
In Asp.net Design Form:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="StudentName"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtStudentName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Province"></asp:Label>
</td>
<td>
<asp:DropDownList ID="ddlProvince" runat="server"></asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
</td>
</tr>
</table>
</form>
</body>
</html>
In Asp.net Back-End :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//bound data from TblProvince to ddlProvince
using (SqlConnection con = DBManager.getConnection())
{
string sql = #"Select ProvinceId, Province From TblProvince";
con.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds, "dtProvince");
ddlProvince.DataSource = ds.Tables["dtProvince"];
ddlProvince.DataValueField = "ProvinceId";
ddlProvince.DataTextField = "Province";
ddlProvince.DataBind();
ddlProvince.Items.Insert(0, new ListItem("Please Select", "NULL"));
}
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
using (SqlConnection con = DBManager.getConnection())
{
string sql = #"Insert Into TblStudent(Name, ProvinceId) Values(#Name, #ProvinceId)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("#Name", SqlDbType.NChar).Value = txtStudentName.Text;
cmd.Parameters.Add("#ProvinceId", SqlDbType.Int).Value = Convert.ToInt32(ddlProvince.SelectedValue);
con.Open();
cmd.ExecuteNonQuery();
}
}
}
Question:
When I do not select in ddlProvince why when u save it to database it shows an error Input string was not in a correct format.
It throws "Input string was not in a correct format" error because you are trying to convert null to Int.
cmd.Parameters.Add("#ProvinceId", SqlDbType.Int).Value = Convert.ToInt32(ddlProvince.SelectedValue);
You can check whether SelectedValue is null before converting it to Int as below.
cmd.Parameters.Add("#ProvinceId", SqlDbType.Int).Value = (ddlProvince.SelectedValue == null) ? null : Convert.ToInt32(ddlProvince.SelectedValue)

asp.net grid view hyper link pass values to new window

hai guys here is my question please help me
I have a gridview with hyperlink fields here my requirement is if I click on hyperlink of particular row I need to display that particular row values into other page in that page user will edit record values after that if he clicks on update button I need to update that record values and get back to previous home page. if iam clicking hyper link in first window new window should open with out any url tool bar and minimize close buttons like popup modular Ajax ModalPopUpExtender but iam un able to ge that that one please help me
the fillowing code is defalut.aspx
<head runat="server">
<title>PassGridviewRow values </title>
<style type="text/css">
#gvrecords tr.rowHover:hover
{
background-color:Yellow;
font-family:Arial;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="gvrecords" AutoGenerateColumns="false"
HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White" DataKeyNames="UserId" RowStyle-CssClass="rowHover">
<Columns>
<asp:TemplateField HeaderText="Change Password" >
<ItemTemplate>
<a href ='<%#"UpdateGridviewvalues.aspx?UserId="+DataBinder.Eval(Container.DataItem,"UserId") %>'> <%#Eval("UserName") %> </a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="Email" HeaderText="Email" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
following code default.aspx.cs code
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
protected void BindGridview()
{
SqlConnection con = new SqlConnection("Data Source=.;Integrated Security=SSPI;Initial Catalog=testdb1");
con.Open();
SqlCommand cmd = new SqlCommand("select * from UserDetails", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.ExecuteNonQuery();
con.Close();
DataSet ds = new DataSet();
da.Fill(ds);
gvrecords.DataSource = ds;
gvrecords.DataBind();
}
}
this is updategridviewvalues.aspx
<head runat="server">
<title>Update Gridview Row Values</title>
<script type="text/javascript">
function Showalert(username) {
alert(username + ' details updated successfully.');
if (alert) {
window.location = 'Default.aspx';
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td colspan="2" align="center">
<b> Edit User Details</b>
</td>
</tr>
<tr>
<td>
User Name:
</td>
<td>
<asp:Label ID="lblUsername" runat="server"/>
</td>
</tr>
<tr>
<td>
First Name:
</td>
<td>
<asp:TextBox ID="txtfname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<asp:TextBox ID="txtlname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Email:
</td>
<td>
<asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnUpdate" runat="server" Text="Update" onclick="btnUpdate_Click" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" onclick="btnCancel_Click"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
and my updategridviewvalues.aspx.cs code is follows
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 UpdateGridviewvalues : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=.;Integrated Security=SSPI;Initial Catalog=testdb1");
private int userid=0;
protected void Page_Load(object sender, EventArgs e)
{
userid = Convert.ToInt32(Request.QueryString["UserId"].ToString());
if(!IsPostBack)
{
BindControlvalues();
}
}
private void BindControlvalues()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from UserDetails where UserId=" + userid, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.ExecuteNonQuery();
con.Close();
DataSet ds = new DataSet();
da.Fill(ds);
lblUsername.Text = ds.Tables[0].Rows[0][1].ToString();
txtfname.Text = ds.Tables[0].Rows[0][2].ToString();
txtlname.Text = ds.Tables[0].Rows[0][3].ToString();
txtemail.Text = ds.Tables[0].Rows[0][4].ToString();
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("update UserDetails set FirstName='" + txtfname.Text + "',LastName='" + txtlname.Text + "',Email='" + txtemail.Text + "' where UserId=" + userid, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
int result= cmd.ExecuteNonQuery();
con.Close();
if(result==1)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "ShowSuccess", "javascript:Showalert('"+lblUsername.Text+"')", true);
}
}
protected void btnCancel_Click(object sender, EventArgs e)
{
Response.Redirect("~/Default.aspx");
}
}
My requirement is new window should come like pop window as new window with out having url and close button
my database tables is
ColumnName DataType
-------------------------------------------
UserId Int(set identity property=true)
UserName varchar(50)
FirstName varchar(50)
LastName varchar(50)
Email Varchar(50)
There are a host of options in the jQuery field:
jQueryUI's dialog
Wijmo's Dialog
SimpleModal
If you'd already using jQuery, jQueryUI's option may be a good fit. Wijmo is also jQueryUI compatible/friendly (use the same theme CSS class names and patterns), so it's also a good fit.
So it kind of depends on what you want. Something very simple - maybe jQueryUI. Flashy/pretty -- SimpleModal. More complex but jQueryUI-friendly - Wijmo.

select index of listView is not working in ASP.NET

I am new to ASP.NET, I am making a search box in my application.
For example: if a user enters "abc" in the textbox, then the textbox will fetch data from the database which starts with "abc". I am passing this data to DataTable.
It works properly,
Here is my code snippet:
DataTable result = new DataTable();
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
connString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
conn.Open();
string query = string.Format("SELECT DISTINCT Scrip FROM dbo.SearchBoxData where Scrip Like '{0}%'", TextBox1.Text);
SqlCommand cmd = new SqlCommand(query, conn);
result.Load(cmd.ExecuteReader());
conn.Close();
lvwItems.DataSource = result;
lvwItems.DataBind();
}
Now I want to retrieve all this data in my <div> tag. so i tried using asp:ListView,
here is my code snippet,
it works properly, but now i want to navigate to new page when user select any row of listView, but i am unable to select any row..
<asp:ListView ID="lvwItems" runat="server" ItemPlaceholderID="plhItems">
<LayoutTemplate>
<div>
<asp:PlaceHolder ID="plhItems" runat="server"></asp:PlaceHolder>
</div>
</LayoutTemplate>
<ItemTemplate>
<div>
<%# Eval("Scrip")%>
</div>
</ItemTemplate>
Thanks In Advance !!
Any help will be appreciated.
EDIT:(SearchBox.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.Data;
public partial class SearchBox : System.Web.UI.Page
{
string connString;
DataTable result = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{ }
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
connString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
conn.Open();
string query = string.Format("SELECT DISTINCT Scrip FROM dbo.SearchBoxData where Scrip Like '{0}%'", TextBox1.Text);
SqlCommand cmd = new SqlCommand(query, conn);
result.Load(cmd.ExecuteReader());
conn.Close();
lvwItems.DataSource = result;
lvwItems.DataBind();
}
protected void lvwItems_SelectedIndexChanging(Object sender, ListViewSelectEventArgs e)
{
ListViewItem item = (ListViewItem)lvwItems.Items[e.NewSelectedIndex];
Label lablId = (Label)item.FindControl("lablId");
if (String.IsNullOrEmpty(lablId.Text))
{
Response.Redirect("NextPage.aspx?id=" + lablId.Text, false);
}
}
(SearchBox.aspx)
<%# Page Language="C#" AutoEventWireup="true" CodeFile="SearchBox.aspx.cs" Inherits="SearchBox" %>
<!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>
</div>
<asp:TextBox ID="TextBox1" runat="server" Height="30px" Width="179px"
OnTextChanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Go"
Width="62px" />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<asp:ListView ID="lvwItems" OnSelectedIndexChanging="lvwItems_SelectedIndexChanging"
runat="server" ItemPlaceholderID="plhItems">
<LayoutTemplate>
<div>
<asp:PlaceHolder ID="plhItems" runat="server"></asp:PlaceHolder>
</div>
</LayoutTemplate>
<ItemTemplate>
<div>
<%# Eval("Scrip")%>
<asp:Label ID="lablId" visible="false" runat="server" Text='<%#Eval("Scrip") %>'/>
</div>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:krunal_DBConnectionString2 %>"
SelectCommand="SELECT * FROM [SearchBoxData]"></asp:SqlDataSource>
</form>
</body>
</html>
You have to add a SELECT button in the ItemTemplate, see the complete working code.
<asp:ListView ID="lvwItems" OnSelectedIndexChanging="lvwItems_SelectedIndexChanging"
runat="server" ItemPlaceholderID="plhItems">
<LayoutTemplate>
<div>
<asp:PlaceHolder ID="plhItems" runat="server"></asp:PlaceHolder>
</div>
</LayoutTemplate>
<ItemTemplate>
<%# Eval("Scrip")%>
<asp:LinkButton ID="SelectButton" runat="server" CommandName="Select" Text="Select" />
</ItemTemplate>
</asp:ListView>
protected void lvwItems_SelectedIndexChanging(Object sender, ListViewSelectEventArgs e)
{
ListViewItem item = (ListViewItem)lvwItems.Items[e.NewSelectedIndex];
Label lablId = (Label)item.FindControl("CONTROL_ID");
}
Thanks
Deepu
Probably you will have to add the event for Selected index change:
<asp:ListView ID="lvwItems" runat="server" ItemPlaceholderID="plhItems" OnSelectedIndexChanging="lvwItems_SelectedIndexChanging">
In code behind you can get the selected row item like below.
Also you can place a label or hidden field so that you can get some data from those control and pass it in to the next page.. (might be id or something).
<asp:ListView ID="lvwItems" OnSelectedIndexChanging="lvwItems_SelectedIndexChanging"
runat="server" ItemPlaceholderID="plhItems">
<LayoutTemplate>
<div>
<asp:PlaceHolder ID="plhItems" runat="server"></asp:PlaceHolder>
</div>
</LayoutTemplate>
<ItemTemplate>
<div>
<%# Eval("Scrip")%>
<asp:Label ID="lablId" visible="flase" runat="server" Text='<%#Eval("Id") %>' />
</div>
</ItemTemplate>
</asp:ListView>
void lvwItems_SelectedIndexChanging(Object sender, ListViewSelectEventArgs e)
{
ListViewItem item = (ListViewItem)lvwItems.Items[e.NewSelectedIndex];
Label lablId = (Label)item.FindControl("lablId");
if (String.IsNullOrEmpty(lablId.Text))
{
Response.Redirect("page.aspx?id="+lablId.Text,false);
}
}
Thanks
Deepu
Here you go,
Also remove the OnClick="callMethod" from the button. Since you have SelectedIndex method no need to have the onClick method.
protected void lvwItems_SelectedIndexChanging(Object sender, ListViewSelectEventArgs e)
{
ListViewItem item = (ListViewItem)lvwItems.Items[e.NewSelectedIndex];
Button btn = (Button)item.FindControl("btn1");
if(btn != null)
{
var buttonText = btn.Text;
}
}
Hope this helps
Thanks
Deepu

Resources