Why is GridView1_SelectedIndexChanged not working?
I'm learning ASP.NET on my own (which means I learn through youtube), using VS 2015.
I created a grid of items and want to show the selected item in a textbox, but code doesn't do any change.
I created an empty project, added a masterpage and default page, and just added a text and grid, scriptmanager updatepanel contenttemplate.
Whenever I click select on any row from the grid textbox is still empty.
MasterPage.master:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Default.aspx:
<%# Page Title="" enableeventvalidation="true" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<style type="text/css">
.auto-style1 {
width: 100%;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table class="auto-style1">
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:CommandField ButtonType="Button" ShowSelectButton="True" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Default.aspx.cs:
using System;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable tbl = new DataTable();
tbl.Columns.Add("serial");
for (int i = 0; i < 4; i++)
{
DataRow row = tbl.NewRow();
row[0] = i + 1;
tbl.Rows.Add(row);
}
GridView1.DataSource = tbl;
GridView1.DataBind();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = GridView1.SelectedRow.Cells[0].Text;
}
}
You are selecting the wrong cell on the SelectedIndexChanged method.
The first Cell (index 0) contains the button, so you want the second cell (index 1).
You should place a breakpoint on the method and see for yourself what other properties are filled in the GridView.
Related
I am trying to use Google Recaptcha on on my web pages. I keep getting an error saying
GoogleReCaptcha.GoogleReCaptcha)(System.Web.HttpRuntime.WebObjectActivator.GetService(typeof(GoogleReCaptcha.GoogleReCaptcha))));
Below is the screen shot:
Below is my aspx page code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TryAgain.WebForm1" %>
<%# Register Assembly="GoogleReCaptcha" Namespace="GoogleReCaptcha" TagPrefix="cc1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>
<h1>Google ReCaptcha Form</h1>
<asp:TextBox ID="txt" runat="server"></asp:TextBox>
<cc1:GoogleReCaptcha ID="ctrlGoogleReCaptcha" runat="server" PublicKey="XXXX" PrivateKey="XXX" />
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
<asp:Label ID="lblStatus" runat="server" Text=""></asp:Label>
<asp:Button ID="btn" runat="server" Text="Submit" OnClick="btn_Click" />
</p>
</div>
</form>
</body>
</html>
Below is my aspx.cs file code:
protected void btn_Click(object sender, EventArgs e)
{
if (ctrlGoogleReCaptcha.Validate())
{
//submit form
lblStatus.Text = "Success";
}
else
{
lblStatus.Text = "Captcha Failed!! Please try again!!";
}
}
GoogleRecaptcha that I downloaded has properties like so:
I am struggling with this error for hours. Any help will be highly appreciated.
i want when click btndelete , every checkbox where chechek in repeater1 to get value of column(titr) in same row, but i dont find which checkbox checked and dont access to value of column(titr)
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
<td> </td>
<td>subject</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:CheckBox ID="CheckBox1" runat="server" />
</td>
<td id="checkvalue" >
<%# Eval("titr") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
<asp:Button ID="btndelete" runat="server" Text="delete" OnClick="btndelete_Click" />
.cs code is :
protected void btndelete_Click(object sender, EventArgs e)
{
}
You could get a list of repeaterItems for each row in which the checkbox is checked using linq such that:
List<RepeaterItem> selectedItems = Repeater1.Items.Cast<RepeaterItem>().Where(x => ((CheckBox)x.FindControl("CheckBox1"))
.Checked).ToList();
You could then iterate through the list and get the value of titr for each of the selected rows, but you would have to set some server control equal to "titr" when you bind the control, like:
<asp:literal id="literal1" runat="server"><%# Eval("titr") %></asp:literal>
That way you could go find the value later when you iterate through the list, like this:
List<string> titrValues = selectedItems.Select(t => ((Literal)t.FindControl("literal1).Text));
You may have to do some tweaking, but that should get you the values of titr for each row with a selected value.
Aspx 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>
</head>
<body>
<form id="form1" runat="server">
<asp:Repeater ID="Repeater1" runat="server" >
<ItemTemplate>
<div>
<asp:CheckBox ID="CategoryID" runat="server" Text='<%# Eval("val") %>' />
</div>
</ItemTemplate>
</asp:Repeater>
<asp:Button Text="Click" OnClick="Button2_Click" runat="server" />
</form>
</body>
</html>
CS Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("val", typeof(string));
for (int i = 0; i < 10; i++)
dt.Rows.Add("testing" + i.ToString());
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string Rpt = "Repeater Items Checked:<br />";
for (int i = 0; i < Repeater1.Items.Count; i++)
{
CheckBox chk = (CheckBox)Repeater1.Items[i].FindControl("CategoryID");
if (chk.Checked)
{
Rpt += (chk.Text + "<br />");
}
}
Response.Write(Rpt);
}
You can also use break point to check the get values....
Refrenced By: http://www.codeproject.com/Questions/534719/GetplusSelectedplusCheckboxesplusinplusASPplusRepe
This question already has an answer here:
Ajax in UserControl
(1 answer)
Closed 8 years ago.
I am using CalenderExtender in my project inside in content place holder and calender extender is in a UserControl. This control is working in normal aspx-page but when i am dragging this control in ContentPlaceHolder then it is not working. Actually the Calender is not appearing in textBox below is my code which i used in my project.
ASPX:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Calender1.ascx.cs" Inherits="Facultymanagement.Calender1" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:TextBox ID="txtcalender" runat="server">
</asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" Format="MM/dd/yyyy"
TargetControlID="txtcalender" PopupButtonID="txtcalender"></asp:CalendarExtender>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
Codebehind:
protected void Page_Load(object sender, EventArgs e)
{
if (CalendarExtender1.SelectedDate.ToString() != "")
{
txtcalender.Text = CalendarExtender1.SelectedDate.ToString();
}
}
public string TextBox1Value
{
get
{
return txtcalender.Text;
// return Convert.ToString(Calendar.SelectedDate);
}
set { txtcalender.Text = value; }
}
}
This is the place where i am trying to access the value:
protected void Button1_Click1(object sender, EventArgs e)
{
Label1.Text = calender1.TextBox1Value.ToString();
}
You need to find control first
ContentPlaceHolder mpContentPlaceHolder1 = (ContentPlaceHolder)Master.FindControl("ContentPlacename");
if (mpContentPlaceHolder1 != null)
{
Button btn_searsh;
btn_searsh = (Button)mpContentPlaceHolder1.FindControl("main_search");
btn_searsh.CssClass += " " + "btn-primary";//to pass the rentpage class
}
I think you must doo something wrong.
The following code works:
WebUserControl1.ascx:
<%# Control Language="C#" ClassName="WebUserControl" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<script runat="server">
</script>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Image ID="Image1" runat="server" />
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="TextBox1" PopupButtonID="Image1">
</cc1:CalendarExtender>
Default.aspx:
<%# Page Language="C#" %>
<%# Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</div>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
<uc1:WebUserControl ID="WebUserControl2" runat="server" />
<uc1:WebUserControl ID="WebUserControl3" runat="server" />
<uc1:WebUserControl ID="WebUserControl4" runat="server" />
</form>
</body>
</html>
I got stucked to some weird condition where I have a gridview inside a ajax toolkit tabcontainer. On tab index change i am binding grid. But nothing happend. Grid is not appearing. I have check the following
Viewstate
Visibility of grid
Visibility of the parent table.
Data is coming from the method
visibility of the tab panel
Even i have debugged and added watch to check if its getting null before loading the page.
Please help me out
** BELOW IS THE UPDATED CODE**
<HTMLCode>
<Toolkit:TabPanel HeaderText="Pending Challans" ID="tpPendingChallan" runat="server" Height="200px" >
<ContentTemplate>
<table style="width: 100%">
<tr>
<td>
<asp:GridView ID="gvPendingChallans" runat="server" AutoGenerateColumns="True" CellPadding="4" Width="100%" OnPageIndexChanging="gvPendingChallans_PageIndexChanging"
OnRowCommand="gvPendingChallans_RowCommand" AllowPaging="True" GridLines="None">
</asp:GridView>
</td>
</tr>
</table>
</ContentTemplate>
</Toolkit:TabPanel>
</HTMLCode>
========================================================================
<C#>
private void BindPendingChallans()
{
var dat = JobCardManager.DisplayChallan();
gvPendingChallans.DataSource = dat;
gvPendingChallans.DataBind();
}
protected void tcMembers_ActiveTabChanged(object sender, EventArgs e)
{
if(tcMembers.ActiveTabIndex == 6)
{
BindPendingChallans();
}
}
</C#>
Sorry for miss interpretation of your code with my first answer. I thought that it just a simple population of grid view, but as review I found that you are using the Ajax Toolkit library and your grid is inside the tab selection. You can try this:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="Toolkit" %>
<!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 id="Head2" runat="server">
<title>Untitled Page</title>
<style type="text/css">
.style1
{
font-family: Arial;
color: #3399FF;
}
</style>
</head>
<body class="style1">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager2" runat="server" EnablePageMethods="true" />
<div>
<asp:UpdatePanel ID="upMember" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<table cellpadding="1" cellspacing="4" border="0" width="100%">
<tr>
<td>
<Toolkit:TabContainer ID="tcMembers" runat="server" AutoPostBack="true"
ActiveTabIndex="0" onactivetabchanged="tcMembers_ActiveTabChanged">
<Toolkit:TabPanel HeaderText="Pending Challans" ID="tpPendingChallan" runat="server"
Height="200px">
<ContentTemplate>
<asp:GridView ID="gvPendingChallans" runat="server" AutoGenerateColumns="True" CellPadding="4"
Width="100%" OnPageIndexChanging="gvPendingChallans_PageIndexChanging" OnRowCommand="gvPendingChallans_RowCommand"
AllowPaging="True" GridLines="None">
</asp:GridView>
</ContentTemplate>
</Toolkit:TabPanel>
<Toolkit:TabPanel HeaderText="Pending 2" ID="tpPending2" runat="server"
Height="200px">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" CellPadding="4"
Width="100%" OnPageIndexChanging="gvPendingChallans_PageIndexChanging" OnRowCommand="gvPendingChallans_RowCommand"
AllowPaging="True" GridLines="None">
</asp:GridView>
</ContentTemplate>
</Toolkit:TabPanel>
</Toolkit:TabContainer>
</td>
<td width="2%">
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
Then in your code behind:
protected void Page_Load(object sender, EventArgs e)
{
upMember.Update();
}
private void BindPendingChallans()
{
//var dat = JobCardManager.DisplayChallan();
//gvPendingChallans.DataSource = dat; gvPendingChallans.DataBind();
}
protected void tcMembers_ActiveTabChanged(object sender, EventArgs e)
{
if (tcMembers.ActiveTabIndex == 6)
{
BindPendingChallans();
}
}
protected void gvPendingChallans_PageIndexChanging(object sender, GridViewPageEventArgs e){
}
protected void gvPendingChallans_RowCommand(object sender, GridViewCommandEventArgs e){
}
Note: That in you 'tcMembers_ActiveTabChanged' you had specify tab index 6
The Tab index begins with 0. Maybe you can adjust it depending the number of
you Intended tab....
Regards,
<%# 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">
<%# Reference Control = "CHEADER.ascx" Page="~/Default.aspx" %>
<%# Reference Control = "CLEFT_NAVIGATION.ascx" %>
<%# Reference Control = "CCOPYRIGHT.ascx" %>
<%# Reference Control = "CBOTTOM_NAVIGATION.ascx" %>
<%# Reference Control = "CPAGE0001.ascx" %>
<%# Reference Control = "CPAGE0002.ascx" %>
<%# Reference Control = "CPAGE0003.ascx" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>WELCOME</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table align="center" style="width: 100%;">
<tr>
<td colspan="2">
<asp:PlaceHolder ID="HEADER" runat="server"></asp:PlaceHolder>
</td>
</tr>
<tr>
<td>
<asp:PlaceHolder ID="LEFT_NAVIGATION" runat="server"></asp:PlaceHolder>
</td>
<td>
<asp:PlaceHolder ID="DETAILS" runat="server"></asp:PlaceHolder>
</td>
</tr>
<tr>
<td colspan="2">
<asp:PlaceHolder ID="COPYRIGHT" runat="server"></asp:PlaceHolder>
</td>
</tr>
<tr>
<td colspan="2">
<asp:PlaceHolder ID="BOTTOM_NAVIGATION" runat="server"></asp:PlaceHolder>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
on .cs code behind i want to use the following code
if(!Page.IsPostBack)
{
WebUserControl1 uc =
(WebUserControl1) Page.LoadControl("WebUserControl1.ascx");
PlaceHolder1.Controls.Add(uc);
}
- but
none of the usercontrols appear thats is if i replace "WebUserControl1" by name CHEADER it says CHEADER? what is that!!
asp.net c# vsts2008
try using register
<%# Register Src="~/CHEADER.ascx" TagName="uc" TagPrefix="webcontrol" %>
protected void Page_Load(object sender, EventArgs e)
{
Control details = LoadControl("WebUserControl.ascx");
PlaceHolder1.Controls.Add(details);
Control details1 = LoadControl("WebUserControl2.ascx");
PlaceHolder2.Controls.Add(details1);
}
this works :-)