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
Anand Solomon
NA
20
0
Gridview within dynamic dropdownlist null exception
Nov 11 2019 8:30 AM
I have a GridView with template fields and one of the template field is a dropdownlist which is dynamically added into the gridview control. It's working properly on page load when I click the button control to the get the values from the template fields it will throw a null reference exception. Please correct my error, I am in pathetic situation. I attached the source code.
Regards
<asp:GridView ID=
"GridView1"
runat=
"server"
AutoGenerateColumns=
"false"
OnRowCreated=
"GridView1_RowCreated"
>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID=
"id"
runat=
"server"
Text=
'<%# Eval("RGNO") %>'
></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID=
"name"
runat=
"server"
Text=
'<%# Eval("NAME") %>'
></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID=
"lastname"
runat=
"server"
Text=
'<%# Eval("DOB") %>'
></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:PlaceHolder ID=
"PlaceHolder1"
runat=
"server"
></asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!IsPostBack) BindGridView();
}
protected
void
GridView1_RowCreated(
object
sender, GridViewRowEventArgs e)
{
if
(e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlResult =
new
DropDownList();
ddlResult.ID =
"ddlResult"
;
ddlResult.Items.Insert(0,
new
ListItem(
"PASS"
,
"0"
));
ddlResult.Items.Insert(1,
new
ListItem(
"FAIL"
,
"1"
));
PlaceHolder p = (PlaceHolder)e.Row.FindControl(
"PlaceHolder1"
);
p.Controls.Add(ddlResult);
}
}
private
void
BindGridView()
{
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
public
DataTable CreateDataSource()
{
DataTable dt =
new
DataTable();
DataRow dr;
dt.Columns.Add(
new
DataColumn(
"RGNO"
,
typeof
(
string
)));
dt.Columns.Add(
new
DataColumn(
"NAME"
,
typeof
(
string
)));
dt.Columns.Add(
new
DataColumn(
"DOB"
,
typeof
(
string
)));
dr = dt.NewRow();
dr[
"RGNO"
] =
"1001"
;
dr[
"NAME"
] =
"PRAKASH"
;
dr[
"DOB"
] =
"10-10-2003"
;
dt.Rows.Add(dr);
return
dt;
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
foreach
(GridViewRow row
in
GridView1.Rows)
{
DropDownList dd1 = (DropDownList)row.FindControl(
"ddlResult"
);
Response.Write(dd1.SelectedItem.Text);
}
}
Reply
Answers (
1
)
OleDbDataReader throws error: Missing Parameters
Using C# to create a Word document with multiple tables