TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
alessandro nesta
NA
20
475
Button update for DropDownList added dinamically in GridView
Feb 24 2021 11:21 AM
Hello everybody. I find myself in difficulty and ask for help for a situation that is often treated, but separately, namely: DDL dynamically created in GridView and event external to GridView that takes some in value / Text ... x stored in the DB.
They are at the first experiences and the code I report often derives from something else found on the net.
**************************************************
In a WebForm I drew a GridView. Outside the GridView is a Button.
The GridView is made up of 2 BoundFields (of which the first is not visible).
This is the page:
<% @ Page Title = "" Language = "C #" MasterPageFile = "~ / Site.Master" AutoEventWireup = "true" CodeBehind = "WebForm2.aspx.cs" Inherits = "ScheduleWeb.WebForm2"%>
<asp: Content ID = "Content1" ContentPlaceHolderID = "HeadContent" runat = "server">
</ asp: Content>
<asp: Content ID = "Content2" ContentPlaceHolderID = "MainContent" runat = "server">
<% - <asp: Panel ID = "panel1" runat = "server" Height = "100px" Width = "930px" ScrollBars = "Horizontal"> -%>
<asp: GridView ID = "GridView2" runat = "server" DataKeyNames = "Codice_fiscale" OnRowDataBound = "GridView_RowDataBound" Width = "938px" AutoGenerateColumns = "False" Font-Names = "Arial" Font-Size = "Smaller" OnRowCreated "GridView2_RowCreated">
<Columns>
<asp: BoundField DataField = "code_fiscale" HeaderText = "code_fiscale" ItemStyle-Width = "280px" ReadOnly = "True" SortExpression = "code_fiscale" Visible = "False">
<ItemStyle Width = "280px"> </ItemStyle>
</ asp: BoundField>
<asp: BoundField DataField = "Surname_Name" HeaderText = "Surname and Name" ItemStyle-Width = "180px" ReadOnly = "True" SortExpression = "Surname_Name">
<ControlStyle Width = "180px" />
<ItemStyle Wrap = "False"> </ItemStyle>
</ asp: BoundField>
</Columns>
<EditRowStyle Font-Names = "Arial" Font-Size = "Small" />
</ asp: GridView>
<% - </ asp: Panel> -%>
& nbsp; _______________________________________________________________________________________ <br />
& nbsp; <br />
<asp: Button ID = "ButtonOK" runat = "server" Text = "Remember" OnClick = "ButtonOK_Click" />
<asp: Label ID = "Label2" runat = "server" BackColor = "# FF6600" Text = "Label"> </ asp: Label>
<br />
</ asp: Content>
*********************************
In the page Load:
1
. I "assume" n°2 DateTime variables that I will need to add the DropDownLists
2
. "populate" the mysql oCombo DataTable that I will need for the combos
3
. if (! This.IsPostBack) {
... "populate" the DataTable oDataTable_2 by filtering my table by x credentials and date range
... loop this DT x generate the TemplateFields. This is the code:
for (int k = 2; k <oDataTable_2.Columns.Count; k ++) // add the day columns from the third column
{
TemplateField tfield = new TemplateField ();
tfield = new TemplateField ();
tfield.HeaderText = oDataTable_2.Columns [k] .ColumnName;
tfield.ItemStyle.Width = 55;
GridView2.Columns.Add (tfield);
}
... I do the DataBind
}
4. This is the code:
protected void GridView_RowDataBound (Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string NomeGiorno = "";
for (int k = 2; k <oDataTable_2.Columns.Count; k ++)
{
NomeGiorno = oDataTable_2.Columns [k] .ColumnName.ToString ();
DropDownList ddlCombo = new DropDownList ();
ddlCombo.ID = NomeGiorno;
ddlCombo.DataSource = oCombo;
ddlCombo.DataTextField = oCombo.Columns [0] .ColumnName;
ddlCombo.DataValueField = oCombo.Columns [0] .ColumnName;
ddlCombo.DataBind ();
string country = (e.Row.DataItem as DataRowView) .Row [DayName] .ToString ();
ddlCombo.Width = 55;
ddlCombo.Font.Name = "Arial";
ddlCombo.Font.Size = 8;
ddlCombo.Items.FindByText (country) .Selected = true;
e.Row.Cells [k] .Controls.Add (ddlCombo);
}
}
GridView2.Columns [1] .HeaderStyle.CssClass = "locked";
}
So far everything ok (or at least outwardly). I use DDL, combining items ... everything OK!
But later I want to store the new situation in the DB and so I will have to build
the update query by cycling the columns of the GridView (which has only one row)
But I can't find control !!
I tried this code ...
protected void ButtonOK_Click (object sender, EventArgs e)
{
string cStringa = "update schedule set";
string NomeGiorno = "";
for (int k = 2; k <GridView2.Columns.Count; k ++) // loop the day columns from the third column
{// ie after tax code, surname and name.
NomeGiorno = GridView2.Columns [k] .ToString ();
string test = ((DropDownList) GridView2.Rows [0] .FindControl (NomeGiorno)). SelectedItem.Text;
.....
.....
}
}
I get the classic error:
System.NullReferenceException: 'Reference to an object not set on an object instance.'
Thanks to all those x will be able to give me a hand
Alessio
Reply
Answers (
3
)
httpStatusCode:not found when trying to Delete
to enable CORS Policy