Generate a table based on a selection from a dynamic drop down? - c#

so I am learning ASP.NET currently and actually today is my second day.
So right now I am wanting to create a page where I have first a dynamically generated dropdown that selects just the first name from my database and table. This I have working
Next I have a gridview table that shows all the data from the database, currently I have hard coded a name so the page will not crash (there are 100000 rows of data)
Lastly when I post my code you will see a checkbox, that is just so when I click it the gridview will show up. I am just trying that out, because that was part of a lesson I learned today.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Test of First Database pull</h1>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" Text="Show Panel" OnCheckedChanged="checkbox1_CheckedChanged" />
<br />
<br />
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="FirstName" DataValueField="FirstName" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ASP.NETConnectionString1 %>" ProviderName="<%$ ConnectionStrings:ASP.NETConnectionString1.ProviderName %>" SelectCommand="SELECT DISTINCT [FirstName] FROM [SampleData] ORDER BY 'FirstName' "></asp:SqlDataSource>
<br />
<br />
<asp:Panel ID="Panel1" runat="server" Visible="False" >
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display.">
<Columns>
<asp:BoundField DataField="SSN" HeaderText="SSN" SortExpression="SSN" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="HomeStreet" HeaderText="HomeStreet" SortExpression="HomeStreet" />
<asp:BoundField DataField="HomeCity" HeaderText="HomeCity" SortExpression="HomeCity" />
<asp:BoundField DataField="HomeState" HeaderText="HomeState" SortExpression="HomeState" />
<asp:BoundField DataField="HomeZip" HeaderText="HomeZip" SortExpression="HomeZip" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:BoundField DataField="CompanyStreet" HeaderText="CompanyStreet" SortExpression="CompanyStreet" />
<asp:BoundField DataField="CompanyCity" HeaderText="CompanyCity" SortExpression="CompanyCity" />
<asp:BoundField DataField="CompanyState" HeaderText="CompanyState" SortExpression="CompanyState" />
<asp:BoundField DataField="CompanyZip" HeaderText="CompanyZip" SortExpression="CompanyZip" />
<asp:BoundField DataField="HomePhone" HeaderText="HomePhone" SortExpression="HomePhone" />
<asp:BoundField DataField="CompanyPhone" HeaderText="CompanyPhone" SortExpression="CompanyPhone" />
<asp:BoundField DataField="CellPhone" HeaderText="CellPhone" SortExpression="CellPhone" />
<asp:BoundField DataField="DateOfBirth" HeaderText="DateOfBirth" SortExpression="DateOfBirth" />
<asp:BoundField DataField="DateOfHire" HeaderText="DateOfHire" SortExpression="DateOfHire" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ASP.NETConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:ASP.NETConnectionString1.ProviderName %>"
SelectCommand="SELECT [SSN], [LastName], [FirstName], [HomeStreet], [HomeCity], [HomeState], [HomeZip], [CompanyName],
[CompanyStreet], [CompanyCity], [CompanyState], [CompanyZip], [HomePhone], [CompanyPhone], [CellPhone],
[DateOfBirth], [DateOfHire] FROM [SampleData] WHERE [FirstName] = 'Bob' "></asp:SqlDataSource>
</asp:Panel>
</div>
</form>
</body>
</html>
Here is my C# part where i call the session
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Demos_GridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string name = DropDownList1.SelectedValue;
Session["FName"] = name;
}
protected void checkbox1_CheckedChanged(object sender, EventArgs e)
{
Panel1.Visible = CheckBox1.Checked;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
As you see I called the session "I believe" I tried using this
<%:Session["user"]%>
within that sql query but it was saying I could not have the <%%> within the asp sql calling.
So if anyone could help me out that would be amazing.

Perhaps you should add the name of the user as a SqlParameter?
SqlDataSource1.SelectParameters.Add(new SqlParameter("#Name", name));
// Of course don't forget to edit your query: WHERE [FirstName] = #Name
Something as the above should work (note I haven't tested it).
Edit:
You could shorten this process even more by configuring your SqlDataSource to set the #Name parameter's value from a Control or Session.

Related

How to take ID of a row in gridview when you press a button in a row

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="IDProject" DataSourceID="SqlDataSource1" GridLines="Vertical" style="margin-top:2rem" Width="1056px">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField SortExpression="ProjectLeader" HeaderText="Project Leader">
<ItemTemplate>
<asp:Label Text='<%# Bind("ProjectLeader") %>' ID="lvProjectLeader" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProjectName" HeaderText="Project Name" SortExpression="ProjectName" />
<asp:TemplateField HeaderText="Start date:" SortExpression="StartDate">
<ItemTemplate>
<asp:Label Text='<%# Bind("StartDate") %>' ID="lbStartDate" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Client Name" SortExpression="Client name">
<ItemTemplate>
<asp:Label Text='<%# Bind("Client name") %>' ID="lbClientName" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employees">
<ItemTemplate>
<asp:Button Text="Open" ID="EmployeesOpen" OnClick="EmployeesOpen_Click" runat="server" CssClass="btn btn-outline-primary" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
InsertCommand="Insert into Project values(#ProjectName, #ClientID,#ProjectLeader,#StartDate)"
SelectCommand="select IDProject, ProjectLeader, ProjectName, StartDate, ClientID, Client.Name from Project inner join Client on IDClient = ClientID"
UpdateCommand="Update Project set ProjectName = #ProjectName where IDProject = #IDProject">
<InsertParameters>
<asp:Parameter Name="ProjectName" />
<asp:Parameter Name="ClientID" />
<asp:Parameter Name="ProjectLeader" />
<asp:Parameter Name="StartDate" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="ProjectName" />
<asp:Parameter Name="IDProject" />
</UpdateParameters>
</asp:SqlDataSource>
I made a gridview table for Projects which has values ProjectName, ClientID, ProjectLeader, StartDate and all the employees that work on that Project. Since i cant fit all the employees that work on a single project inside a small table i made a button inside that column that redirects you to another page where you can see table with all the employees. The employees are in another table in sql and i made third table which connects Projects and employees and it only contains ProjectID and EmployeeID. I have a button inside every row but i cant find a way how to make in code behind so that it takes the id of that project and prints on another page only employees that are working on that project.
public partial class Project : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void EmployeesOpen_Click(object sender, EventArgs e)
{
Response.Redirect("Employees_On_Project.aspx");
}
}
You will find what you are looking for here : https://forums.asp.net/t/1957409.aspx?Gridview+get+SelectedRow+in+button+click+event

GridView Asp .Net-Changing the selected value of a second drop down list based on value of first drop Down in same column

I'm new to ASP.NET and C#. I have a Grid View With Drop-Down-list.For Instance if the Grid View is like this,
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="`server`">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Designation" HeaderText="Designation" SortExpression="Designation" />
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="City" DataValueField="City">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:CS %>" SelectCommand="SELECT [City] FROM [EmployeeDetails]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CS %>" SelectCommand="SELECT [ID], [Name], [Designation] FROM [EmployeeDetails]"></asp:SqlDataSource>
</form>
And In Column City,If suppose I select Mumbai in first DropDown, I want rest of the Dropdownlists in the column City to automatically change to Mumbai.
How to do that?
Change as below in you aspx file,
<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="City" DataValueField="City" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
In you aspx.cs file,
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList selectedDropDown = (DropDownList)sender;
foreach(GridViewRow gRow in GridView1.Rows)
{
DropDownList ddlCity = (DropDownList)gRow.FindControl("DropDownList1");
ddlCity.SelectedValue = selectedDropDown.SelectedValue;
}
}
This should work for all the drop down.
If you want it to work only for First Dropdown then, Use the condition as below.
if (((GridViewRow)selectedDropDown.Parent.Parent).RowIndex == 0)
{
foreach (GridViewRow gRow in GridView1.Rows)
{
DropDownList ddlCity = (DropDownList)gRow.FindControl("DropDownList1");
ddlCity.SelectedValue = selectedDropDown.SelectedValue;
}
}

How to refresh gridview after insertion in asp.net c#

I am using gridview in a page of website that I am builing with asp.net so I am using sqldatasource component to fill gridview.now I insert new data into the table and page is refreshed but new data is not displayed in the gridview how to solve this problem
Bind your gridview again after inserting new data. It will take time but you have to fetch the data from database and bind it again to gridview.
Gridview.DataSource = yourDataSource;
Gridview.DataBind();
Bind the Grid in page_Init()
It bind the data from your database Every page init
Documentation on inserting using SqlDataSource - SqlDataSource.Insert Method()
And here's a complete, wroking example:
Code behind:
public partial class SqlDataSourceExample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnInsert_Click(object sender, EventArgs e)
{
SqlDataSource1.Insert();
}
}
.ASPX:
<form id="form1" runat="server">
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT [ID], [Name], [Surname] FROM [Person]"
InsertCommand="INSERT INTO [Person](Name,Surname)VALUES(#name,#surname)">
<InsertParameters>
<asp:ControlParameter ControlID="txtName" Name="name" />
<asp:ControlParameter ControlID="txtSurname" Name="surname" />
</InsertParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Surname" HeaderText="Surname" SortExpression="Surname" />
</Columns>
</asp:GridView>
<asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
<asp:Label ID="lblSurname" runat="server" Text="Surname"></asp:Label>
<asp:TextBox ID="txtSurname" runat="server"></asp:TextBox><br />
<asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="btnInsert_Click" />
</form>

asp.net GridView table won't display if container div is set invisible during page load

I have two GridViews in an ASP WebForm project, one for display and one for edit. I have them each in a separate Div. I start with the Edit div invisible, and have a button in the display div that makes the display div invisible and the edit div visible. The basic code looks like this:
<div id="DisplayDiv">
<asp:GridView ID="CertList" runat="server" AutoGenerateColumns="False" DataSourceID="GetMyData">
<Columns>
<asp:BoundField DataField="a few datafields go here />
</Columns>
</asp:GridView>
<asp:Button ID="btnEdit" runat="server" OnClick="btnEdit_Click" Text="Edit" />
</div>
<div id="EditDiv" visible="false">
<asp:GridView ID="CertList" runat="server" AutoGenerateColumns="False" DataSourceID="GetMyData" ">
<Columns>
<asp:BoundField DataField="a few datafields go here />
</Columns>
</asp:GridView>
</div>
The click event for the button looks like this:
protected void btnEdit_Click(object sender, EventArgs e)
{
EditDiv.Visible = true;
DisplayDiv.Visible = false;
}
Everything works fine with the plumbing if I don't set the EditDiv's visible attribute to false in the markup. If I do set it to false, the table doesn't show when I set it back to true programmatically in the button click event. It seems that the DataView's rendering capability is tied to the ability to access the markup. So, based on that theory, I tried setting the position to absolute and the left to -10000 and I got the same result.
Is this just something I can't do, or am I missing something?
Edit: I put this test together at home and it works fine:
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div id="Div1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="CustomerID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True"
SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName"
SortExpression="ContactName" />
<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle"
SortExpression="ContactTitle" />
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Button1" />
</div>
<div id="Div2" runat="server" visible="false">
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="ProductID" DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID"
InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName"
SortExpression="ProductName" />
<asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock"
SortExpression="UnitsInStock" />
</Columns>
</asp:GridView>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text="Button2" />
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT TOP (5) CustomerID, CompanyName, ContactName, ContactTitle FROM Customers">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT TOP (5) * FROM Products">
</asp:SqlDataSource>
</asp:Content>
And the code behind:
protected void Button1_Click(object sender, EventArgs e)
{
Div1.Visible = false;
Div2.Visible = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
Div2.Visible = false;
Div1.Visible = true;
}
So there's a proof of concept. It's probably a silly mistake, but I'll post it once I find out what the story is.
Set runat="server" for both the divs then try to access from the code behind.
<div id="DisplayDiv" runat="server">
....
</div>
<div id="EditDiv" runat="server" visible="false">
....
</div>
Or you can use a asp:Panel instead of a div then you can access the Visible property from code behind.
<asp:Panel Id ="PnlDisplay" runat="server">
....
</asp:Panel>
<asp:Panel Id ="PnlEdit" runat="server" Visible="false">
....
</asp:Panel>
And you code behind
protected void btnEdit_Click(object sender, EventArgs e)
{
PnlDisplay.Visible = false;
PnlEdit.Visible = true;
}
The problem turned out to be due to a strange idea I had in the SQLDataSource. The markup looks like this (Note: there is no ID in the table with a value of 0, that's a dummy value):
<asp:SqlDataSource ID="foo" runat="server"
ConnectionString="<%$ ConnectionStrings:BlahBlah %>"
SelectCommand="SELECT blahblahblabbertyblah from blah
WHERE ID = '0'>
</asp:SqlDataSource>
And then my code behind has this little masterpiece:
protected void btnEdit_Click(object sender, EventArgs e)
{
DisplayDiv.Visible = false;
EditDiv.Visible = true;
string SqlSelect =
"SELECT c.CourseCode AS [Course Code], c.Description, ce.CertDate AS [Date Certified], ce.Recert, " +
"DATEADD(m, c.Period, ce.CertDate) AS [Expiration Date], e.DocLink, e.CertifiedTrainer, e.GroupLeader " +
"FROM Certifications c " +
"INNER JOIN CertificationEmployees ce ON c.ID = ce.CertID " +
"RIGHT JOIN Employees e ON ce.EmpID = e.ID " +
"WHERE e.ID = '" + EmployeeList.SelectedValue + "' " +
"ORDER BY [Course Code]";
EditCertifications.SelectCommand =
"SELECT blahblahblabbertyblah from blah " +
"WHERE e.ID = '" + aListImUsing.SelectedValue + "' " +
DataView eView = (DataView)EditCertifications.Select(DataSourceSelectArguments.Empty);
eTable = eView.ToTable();
//use the eView table to set various onscreen values
}
As it turned out, every time I did anything that required a screen change, the GridView mysteriously disappeared. I added a CommandField column to the GridView, and when I clicked on the Edit button, I'd lose the GridView. Turned out that the GridView was null. I was thinking that if I respecified the SelectCommand programmatically, it would persist. It doesn't; it reverts back to the one specified in the markup as soon as wherever you've specified it in code goes out of scope.
The fix was to change the dummy value in the WHERE clause to a parameter as I'm supposed to do, and tie the parameter to the current value in the dropdown list that I'm using. I changed the WHERE clause to this:
WHERE e.ID = #EmpID
and added this:
<SelectParameters>
<asp:ControlParameter ControlID="EmployeeList" Name="EmpID" PropertyName="SelectedValue" />
</SelectParameters>
And my GridViews magically appeared.

How to use ModalPopUpExtender in GridView?

I have following tables in my database:
SuggestionsLog Table: ID, Title, Description.
Employee Table: Username, Name
Divisions Table: DivisionCode, DivisionName
I want to show table that consists of SuggestionID, SuggestionTitle, EmployeeName and DivisionName only and when the user clicks on the SuggestionTitle, a pop-up window will be displayed with the description of the suggestion.
Since I am ASP.NET beginner developer, I tried to follow this tutorial to get it but I failed.
What I did is the following:
ASP.NET Code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkSuggestionTitle"
Text='<%#Eval("Title") %>'
OnClick="lnkSuggestionTitle_Click">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<%--<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />--%>
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:BoundField DataField="DivisionName" HeaderText="Division"
SortExpression="DivisionName" />
</Columns>
</asp:GridView>
<asp:Button runat="server" ID="btnModalPopUp" style="display:none" />
<AjaxToolkit:ModalPopupExtender ID="modalPopUpExtender1"
runat="server" TargetControlID="btnModalPopUp" PopupControlID="pnlPopUp" BackgroundCssClass="modalBackground"
OkControlID="btnOk" X="20" Y="100">
</AjaxToolkit:ModalPopupExtender>
<asp:Panel runat="server" ID="pnlPopUp" CssClass="confirm-dialog">
<asp:GridView ID="GridView2" runat="server">
</asp:GridView>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT dbo.SafetySuggestionsLog.ID, dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.employee.Name, dbo.SafetySuggestionsLog.Username,
dbo.Divisions.DivisionName
FROM dbo.employee INNER JOIN
dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN
dbo.Divisions ON dbo.employee.DivisionCode = dbo.Divisions.SapCode">
</asp:SqlDataSource>
Code-Behind:
protected void lnkSuggestionTitle_Click(object sender, EventArgs e)
{
LinkButton lnkSuggestionTitle = sender as LinkButton;
string strSuggestionTitle = lnkSuggestionTitle.Text;
string strConnectionString = ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;
string strSelect = "SELECT ID, Title, Description FROM dbo.SafetySuggestionsLog";
SqlConnection sqlCon = new SqlConnection();
sqlCon.ConnectionString = strConnectionString;
SqlCommand cmdSuggestionDetails = new SqlCommand();
cmdSuggestionDetails.Connection = sqlCon;
cmdSuggestionDetails.CommandType = System.Data.CommandType.Text;
cmdSuggestionDetails.CommandText = strSelect;
cmdSuggestionDetails.Parameters.AddWithValue("#Title", strSuggestionTitle);
sqlCon.Open();
SqlDataReader dReader = cmdSuggestionDetails.ExecuteReader();
GridView1.DataSource = dReader;
GridView1.DataBind();
sqlCon.Close();
modalPopUpExtender1.Show();
}
Everything is going well and smooth, but in the website, when I clicked on one of the titles, I did not get the ModalPopUp. Also, I got an error notification at the left bottom corner in the Internet Explorer browser, which when I opened it, it gave me the following description:
**
Sys.ArgumentNullException: Value cannot be null. Parameter name:
elements
**
I don't know why this is happened. Any help please?
This issue is usually a bad OkControlID or CancelControlID (via here), which could be "btnOk" in this case.
Following on from #PeterX's suggestion, yes your Panel declaration is missing a lot of things.
I would redo this like something as this, which is what I use inside another even bigger GridView control.
<ajaxToolkit:ModalPopupExtender ID="mpeEndorsed" runat="server"
BackgroundCssClass="modalBackground"
PopupControlID="pnlEndorsed"
OkControlID="btnEndorsed"
CancelControlID="btnNotEndorsed"
PopupDragHandleControlID="dvHdr"
Drag="true"
TargetControlID="cbEndorsed">
</ajaxToolkit:ModalPopupExtender>
<asp:Panel ID="pnlEndorsed" runat="server" CssClass="pnlEndorsed" style="display:none">
<div id="dvHdr">
<asp:Label ID="Label3" runat="server">** CONTACT LOG **</asp:Label>
</div>
<div id="dvBody">
<table style="text-align:center; width:100%">
<tr>
<asp:GridView ID="gvContactLog" runat="server" ForeColor="#333333" GridLines="None" AllowPaging="true" PageSize="8" Font-Size="X-Small" CellPadding="4" AllowSorting="True" AutoGenerateColumns="False">
</asp:GridView>
</tr>
<tr>
<td>
<asp:Button ID="btnEndorsed" runat="server" Text="YES" />
<asp:Button ID="btnNotEndorsed" runat="server" Text="NO" />
</td>
</tr>
</table>
</div>
</asp:Panel>
Note the GridView inside the pop-up Panel is a skeleton, that has to be built up either from JavaScript or CodeBehind to get it to be useful.
Defining a Gridview in its entirety (with DataSource, fields, etc) inside a pop-up panel, that's inside another even bigger Gridview (as in my case), does not work! You will get a muddled looking screen and things all over the place. I guess it's a limitation of the AjaxToolKit, to mix client-side and server-side functionality.

Categories

Resources