Dropdownlist in repeater update panel losing value after postback - asp.net

Dear all i have the following dropdownlist which is inside an update panel inside a repeater.
<asp:Repeater OnItemDataBound="rprProperties_ItemDataBound" ID="rprProperties" runat="server">
<ItemTemplate>
<div class="mb-2">
<asp:Label style="width : 100px;float:left;" ID="Label1" runat="server" Text='<%# Container.DataItem("name") %>'></asp:Label>
<asp:Label style="width : 100px;float:left;" ID="propID" runat="server" Text='<%# Container.DataItem("id") %>' Visible="false"></asp:Label>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:DropDownList ClientIDMode="AutoID" AutoPostBack="true" OnSelectedIndexChanged="ddlProperty_SelectedIndexChanged" style="width:100px" CssClass="filter-dropdown bg-light" DataValueField="id" DataTextField="name" ID='ddlProperty' runat="server"></asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</ItemTemplate>
</asp:Repeater>
i'm populating the ddl with this code in the ItemDataBound event of the repeater.
Dim propID As Label = TryCast(e.Item.FindControl("propID"), Label)
Dim ddl As DropDownList = TryCast(e.Item.FindControl("ddlProperty"), DropDownList)
Dim varDbconn As New SqlConnection(ConfigurationManager.ConnectionStrings("shopCS").ToString)
Dim varDbcomm As SqlCommand
Dim varDbRead As SqlDataReader
varDbconn.Open()
varDbcomm = New SqlCommand("exec spShowItemPropValues #property,#id,#lang ", varDbconn)
varDbcomm.Parameters.AddWithValue("#property", SqlDbType.Int).Value = propID.Text
varDbcomm.Parameters.AddWithValue("#id", SqlDbType.Int).Value = Request.QueryString("id")
varDbcomm.Parameters.AddWithValue("#lang", SqlDbType.NVarChar).Value = Session("lang")
varDbRead = varDbcomm.ExecuteReader()
Dim varDt As New DataTable
varDt.Load(varDbRead)
ddl.DataSource = varDt
ddl.DataBind()
varDbcomm.Dispose()
varDbRead.Close()
varDbconn.Close()
when i select a value, the dropdownlist resets to the first item in the dropdownlist instead of keeping the selected value.
i want to retain that value.
thanks.

You forgot to add the AsyncPostBackTrigger. Put it before </UpdatePanel>
<asp:Repeater OnItemDataBound="rprProperties_ItemDataBound" ID="rprProperties" runat="server">
<ItemTemplate>
<div class="mb-2">
<asp:Label style="width : 100px;float:left;" ID="Label1" runat="server" Text='<%# Container.DataItem("name") %>'></asp:Label>
<asp:Label style="width : 100px;float:left;" ID="propID" runat="server" Text='<%# Container.DataItem("id") %>' Visible="false"></asp:Label>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:DropDownList ClientIDMode="AutoID" AutoPostBack="true" OnSelectedIndexChanged="ddlProperty_SelectedIndexChanged" style="width:100px" CssClass="filter-dropdown bg-light" DataValueField="id" DataTextField="name" ID='ddlProperty' runat="server">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlProperty" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
</ItemTemplate>
</asp:Repeater>

Related

Update panel not working inside Gridview - Asp.Net/VB.Net

When I click the image button 'imgPaymentMethod' I require the code to change the ImageUrl of the Image Button. However, nothing happens when clicked. I have also tried putting the entire gridview inside the update panel but then I get this error:
A control with ID 'imgPaymentMethod' could not be found for the
trigger in UpdatePanel 'upPnlControls'
Front End..
<asp:GridView ID="gdvItems" runat="server" AutoGenerateColumns="False"
DataKeyNames="fileID" DataSourceID="DSUploadedItems" CssClass="mGrid"
AlternatingRowStyle-CssClass="alt">
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
<Columns>
<asp:TemplateField visible="true" HeaderText="Price">
<ItemTemplate>
<asp:DropDownList ID="ddlBuyPrice" runat="server" AutoPostBack="True" Font-Size="11px" Height="22px" OnSelectedIndexChanged="ddlBuyPrice_SelectedIndexChanged" SelectedValue='<%# Bind("buyPrice")%>'>
<asp:ListItem Value="0.00">Select:</asp:ListItem>
</asp:DropDownList>
<asp:LinkButton visible="false" ID="lnkPaymentMethod" runat="server" CausesValidation="False"
Text='<%# Bind("acceptPaypal")%>' > /></asp:LinkButton>
<br /><br />
<asp:Label ID="lblSoldStatus" visible="false" runat="server" CausesValidation="False" Text='<%# Bind("sold") %>' />
<asp:Image ID="imgSoldStatus" alt="Sold Status" runat="server" width="100px" ImageUrl="~/files/images/icons/iconSold.png" />
<br />
<asp:UpdatePanel ID="upPnlControls" style="margin-top:0px;" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<Triggers>
<asp:AsyncPostBackTrigger controlid="imgPaymentMethod" eventname="Click" />
</Triggers>
<ContentTemplate>
<asp:ImageButton visible="true" ID="imgPaymentMethod" runat="server" CausesValidation="False" width="50px" onclick="lnkPaymentMethod_Click"></asp:ImageButton>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<asp:Label ID="lblDateBought" runat="server" CausesValidation="False" Text='<%# Bind("dateBought") %>' />
<asp:LinkButton ID="lnkMarkAsSold" runat="server" OnClick="markAsSold_Click" Text="Mark As Sold" Visible="true" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
VB Code..
Protected Sub lnkpaymentMethod_Click(sender As Object, e As System.EventArgs)
Dim imgPaymentMethod As ImageButton = CType(sender, ImageButton)
Dim currentRow As GridViewRow = DirectCast(imgPaymentMethod.Parent.Parent, GridViewRow)
If imgPaymentMethod.ImageUrl="~/files/images/icons/paypalIcon.gif" Then
imgPaymentMethod.ImageUrl="~/files/images/icons/cashIcon.gif"
Else
imgPaymentMethod.ImageUrl="~/files/images/icons/paypalIcon.gif"
End If
End Sub
After discussion we had in comments, if I understood you correctly the problem here is this line:
Dim currentRow As GridViewRow = DirectCast(imgPaymentMethod.Parent.Parent, GridViewRow)
If you want to get grid view row that you click via imgPaymentMethod button, you need to use NamingContainer instead of Parent as follow:
Dim currentRow As GridViewRow = DirectCast(imgPaymentMethod.NamingContainer, GridViewRow)

Repeater Autopostback Textbox Focus Position issue

I have a repeater control that contains a Textbox within an UpdatePanel, as well as other controls. The repeater fetches data from an SQL database and when the user makes changes to this specific textbox, the OnTextChange event is fired when the user "tabs out" of the text box. This event triggers a Stored Procedure code to update the databse with the new values in the textbox. The functionality I'd like to implement is when the user tabs out of the text box, the next text box in the repeater row should be focused on for continuous editing (similar to an excel spreadsheet) after the AutoPostBack from the OnTextChange event is fired. Would anyone have an example on how to do this using the code i have provided below? Thank you in advance!
Form Markup:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Repeater ID="GradeEditor" runat="server">
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="True">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server" Width="100%" Height="100%">
<table style="width:100%;">
<tr>
<td>
<asp:Label ID="LateLabel" runat="server" CssClass="label label-success" Visible="False"></asp:Label>
</td>
<td> </td>
</tr>
<tr>
<td colspan="2">
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" BackColor="Transparent" BorderColor="Transparent" OnTextChanged="Grade_TextChanged" style="text-align: center" Text='<%# Bind("Grade_Code") %>' Width="100%"></asp:TextBox>
</td>
</tr>
</table>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("ID") %>' Visible="False"></asp:Label>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("ID") %>' />
<asp:HiddenField ID="HiddenField2" runat="server" Value='<%# Bind("Class_ID") %>' />
<asp:HiddenField ID="HiddenField3" runat="server" Value='<%# Bind("Assignment_ID") %>' />
<asp:HiddenField ID="HiddenField4" runat="server" Value='<%# Bind("Student_ID") %>' />
<asp:HiddenField ID="HiddenField5" runat="server" Value='<%# Bind("Exempt") %>' />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:Repeater>
Code Behind for Update:
Protected Sub Grade_TextChanged(sender As Object, e As EventArgs)
'Find the reference of the Repeater Item.
Dim ScoreText As TextBox = CType(sender, TextBox)
Dim item As RepeaterItem = CType(ScoreText.NamingContainer, RepeaterItem)
Dim ScoreID As Integer = Integer.Parse(TryCast(item.FindControl("HiddenField1"), HiddenField).Value)
Dim ClassID As Integer = TryCast(item.FindControl("HiddenField2"), HiddenField).Value.Trim()
Dim AssignmentID As String = TryCast(item.FindControl("HiddenField3"), HiddenField).Value.Trim()
Dim StudentID As String = TryCast(item.FindControl("HiddenField4"), HiddenField).Value.Trim()
Dim Grade As String = TryCast(item.FindControl("TextBox1"), TextBox).Text
Dim constr As String = ConfigurationManager.ConnectionStrings("AthenaConnectionString").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("Scores_CRUD")
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("#Action", "UPDATE")
cmd.Parameters.AddWithValue("#Score", Grade)
cmd.Parameters.AddWithValue("#ScoreID", ScoreID)
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Me.BindRepeater()
End Sub
You have an issue in your markup with UpdatePanel. The UpdatePanel should have the Repeater control within its ContentTemplate as in markup below.
Change your code to as given below.
Correct Markup
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="True">
<ContentTemplate>
<asp:Repeater ID="GradeEditor" runat="server">
<ItemTemplate>
<asp:Panel ID="Panel1" runat="server" Width="100%" Height="100%">
<table style="width:100%;">
<tr>
<td>
<asp:Label ID="LateLabel" runat="server" CssClass="label label-success" Visible="False"></asp:Label>
</td>
<td> </td>
</tr>
<tr>
<td colspan="2">
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" BackColor="Transparent" BorderColor="Transparent" OnTextChanged="Grade_TextChanged" style="text-align: center" Text='<%# Bind("Grade_Code") %>' Width="100%"></asp:TextBox>
</td>
</tr>
</table>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("ID") %>' Visible="False"></asp:Label>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("ID") %>' />
<asp:HiddenField ID="HiddenField2" runat="server" Value='<%# Bind("Class_ID") %>' />
<asp:HiddenField ID="HiddenField3" runat="server" Value='<%# Bind("Assignment_ID") %>' />
<asp:HiddenField ID="HiddenField4" runat="server" Value='<%# Bind("Student_ID") %>' />
<asp:HiddenField ID="HiddenField5" runat="server" Value='<%# Bind("Exempt") %>' />
</asp:Panel>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
Then, you need to emit JavaScript for focusing the next textbox from the text changed event of textbox in Repeater. This can be done using C# code as below.
Text changed event C# code for focusing the next box
protected void Grade_TextChanged(object sender, EventArgs e) {
//PUT your original code for calling stored procedure here
TextBox textBox = sender as TextBox;
BindRepeater();
RepeaterItem currentRepeaterItem = (RepeaterItem)(textBox.NamingContainer);
int currentRepeaterItemIndex = currentRepeaterItem.ItemIndex;
RepeaterItem nextRepeaterItem = null;
//get the next item row of Repeater
if ((rptCustomers.Items.Count - 1) >= (currentRepeaterItemIndex + 1)) {
nextRepeaterItem = GradeEditor.Items[currentRepeaterItemIndex + 1];
}
if (nextRepeaterItem != null) {
//get the textbox in next row of Repeater
TextBox nextTextBox = nextRepeaterItem.FindControl("TextBox1") as TextBox;
//emit JavasSript to focus the next textbox
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "focusnext", String.Format(" var nextTextBox = document.getElementById('{0}'); nextTextBox.focus(); nextTextBox.select();", nextTextBox.ClientID), true);
}
}

UpdatePanel not updating ListBox

I have an UpdatePanel containing a ListBox. Whenever I change selection on from a DropDown I want the list to get updated via an UpdatePanel. However this is not working.
This is my code so far:
Protected Sub drpDepartments_SelectedIndexChanged(sender As Object, e As EventArgs) Handles drpDepartments.SelectedIndexChanged
Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
Dim deptComm As String = "SELECT department_id FROM departments WHERE department_name = #DepartmentName"
Dim deptSQL As New SqlCommand
Dim dr As SqlDataReader = deptSQL.ExecuteReader()
deptSQL = New SqlCommand(deptComm, conn)
deptSQL.Parameters.AddWithValue("#DepartmentName", drpDepartments.SelectedItem.Text)
dr.Read()
If dr.HasRows Then
Dim department_id As Integer = Convert.ToInt32(dr("department_id"))
Session("DepartmentID") = department_id
End If
dr.Close()
conn.Close()
ASP
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:ListBox ID="lstDeptMembers" runat="server" DataSourceID="SqlDataSource4" DataTextField="name" DataValueField="name" Height="176px" Width="204px"></asp:ListBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="drpDepartments" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" SelectCommand="SELECT * FROM users u
INNER JOIN
(
Select x.user_id as userid,x.department_id,y.department_name
from user_Department x
inner join departments y
on x.department_id=y.department_id WHERE x.department_id=#parameter
)
f on u.user_id= f.userid">
<SelectParameters>
<asp:SessionParameter Name="parameter" SessionField="DepartmentID" />
</SelectParameters>
</asp:SqlDataSource>
DropDownList:
<asp:DropDownList ID="drpDepartments" runat="server" DataSourceID="SqlDataSource3" DataTextField="department_name" DataValueField="department_name">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" SelectCommand="SELECT [department_name] FROM [departments]"></asp:SqlDataSource>
Screenshot:
How can I make it that the ListBox updates whenever a new selection is clicked from the DropDown ?
EDIT: Code for the table that contains the ListBox etc.. :
<asp:DropDownList ID="drpDepartments" runat="server" DataSourceID="SqlDataSource3" DataTextField="department_name" DataValueField="department_name">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" SelectCommand="SELECT [department_name] FROM [departments]"></asp:SqlDataSource>
<br />
<br />
<table style="width:100%;">
<tr>
<td style="width: 221px">
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:ListBox ID="lstDeptMembers" runat="server" DataSourceID="SqlDataSource4" DataTextField="name" DataValueField="name" Height="176px" Width="204px"></asp:ListBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="drpDepartments" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" SelectCommand="SELECT * FROM users u
INNER JOIN
(
Select x.user_id as userid,x.department_id,y.department_name
from user_Department x
inner join departments y
on x.department_id=y.department_id WHERE x.department_id=#parameter
)
f on u.user_id= f.userid">
<SelectParameters>
<asp:SessionParameter Name="parameter" SessionField="DepartmentID" />
</SelectParameters>
</asp:SqlDataSource>
</td>
<td style="width: 398px">
<asp:TextBox ID="txtuserSearch" runat="server"></asp:TextBox>
<asp:Button ID="btnSearchDeptUser" runat="server" Text="Search" />
<br />
<asp:Label ID="lblAddDeptUser" runat="server" Visible="False"></asp:Label>
</td>
</tr>
<tr>
<td >
<asp:Button ID="btnRmvDeptMem" runat="server" Text="Remove" />
</td>
<td>
<asp:Button ID="btnAddDeptUser" runat="server" Text="Add" />
</td>
</tr>
</table>
Full code for Departments.aspx
If you want to update the UpdatePanel immediately when the user selects an item in the DropDownList you have to set it's AutoPostBack property to "True"(default is false):
<asp:DropDownList ID="drpDepartments" runat="server" DataSourceID="SqlDataSource3"
AutoPostack="True" DataTextField="department_name" DataValueField="department_name">
</asp:DropDownList>
try this...
Change this
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
with
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">

Ajax Extension (Timer) stopped working when I retrieve value from DataTable

Currently I have an Ajax Timer which executes a function every few second. It is working perfectly fine until I tried to retrieve data from a DataTable. I have no idea why. I have tried debugging. I even placed label on the page to check.
For example the Ajax Timer:
Protected Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
Label1.Text = DateTime.Now.ToLongTimeString()
End Sub
The datalist (the one giving the problem):
Protected Sub dlOrgProfile_ItemCreated(sender As Object, e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlOrgProfile.ItemCreated
Dim bizLayerMgmt As BlOrganizations
Dim dt As DataTable
bizLayerMgmt = New BlOrganizations()
dt = bizLayerMgmt.getOrgDetails(userId).Tables(0)
ddl = CType(e.Item.FindControl("ddlCoType"), DropDownList)
Dim value As Integer = Convert.ToInt32(dt.Rows(0)(3)) 'I have narrowed the problem to this line, if I comment this line.. everything works perfectly
ddl.SelectedValue = value
End Sub
The problem lies in the dt.Rows(0)(3). I have no idea why. I need to use it to retrieve some data from the database.
Just in case if its the front-end side.. here's the markup for the site.
<div class="content">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Interval="1000">
</asp:Timer>
<asp:DataList ID="dlOrgProfile" runat="server" DataSourceID="odsOrgDetails"
EnableTheming="True" RepeatLayout="Flow" ShowFooter="False" ShowHeader="False">
<ItemTemplate>
<h3>
<asp:TextBox ID="txBxCoName" runat="server" Text='<%# Eval("OrgName") %>'></asp:TextBox>
<div class="ddlSelect">
<asp:DropDownList ID="ddlCoType" runat="server" DataSource='<%# listOrgType() %>' DataTextField="OrganizationType" DataValueField="OrgTypeID" >
</asp:DropDownList>
</div>
<h3>
</h3>
<a id="linkCoImg" href="upload_co_logo.aspx">
<asp:Image ID="CoImg" runat="server" ImageUrl="~/logo/org/default.png" />
<span>Change</span> </a>
<br />
<div id="description">
<textarea id="taCoDesc" rows="2" cols="1"><%# Eval("Description") %></textarea>
</div>
<br />
<asp:Label ID="lblContacts" runat="server" Text="Contacts:"></asp:Label>
<br />
<div id="contacts">
<asp:TextBox ID="tbContactOffice" runat="server" CssClass="tbContacts"></asp:TextBox>
<asp:TextBox ID="tbContactFax" runat="server" CssClass="tbContacts"></asp:TextBox>
<asp:TextBox ID="tbContactMail" runat="server" CssClass="tbContacts2"></asp:TextBox>
</div>
<br />
<asp:Button ID="btnSave" runat="server" CommandArgument='<%# Eval("OrgID") %>'
CommandName="save" Text="Save" />
</h3>
</ItemTemplate>
</asp:DataList>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
Okay I have resolved my own problem.. Still don't really get it why but this is what I did.. Instead of using ItemCreated, I used ItemDataBound.
Protected Sub dlOrgProfile_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlOrgProfile.ItemDataBound
Dim bizLayerMgmt As BlOrganizations
Dim dt As DataTable
bizLayerMgmt = New BlOrganizations()
dt = bizLayerMgmt.getOrgDetails(userId).Tables(0)
ddl = CType(e.Item.FindControl("ddlCoType"), DropDownList)
Dim value As Integer = Convert.ToInt32(dt.Rows(0)(3))
ddl.SelectedValue = value
End Sub

GridView Error: Object reference not set to an instance of an object

have a gridview with template fields, one column contains a dropdownlist that should be populated with a sql statement. I have created the grid dynamically and called the rowdatabound in order to access the dropdownlist but I keep recieving the error: Object reference not set to an instance of an object. Anyone have any ideas?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:CommandField EditText="Add" ShowEditButton="True" />
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:Label ID="Label6" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This is the code for the creation of the GridView Dynamically, this is done in the page_load event:
If Not Page.IsPostBack Then
Dim CommittedTable As New DataTable("Committed")
CommittedTable.Columns.Add("Date Posted", GetType(Date))
CommittedTable.Columns.Add("Vendor", GetType(String))
CommittedTable.Columns.Add("Expense Description", GetType(String))
CommittedTable.Columns.Add("Ledger", GetType(String))
CommittedTable.Columns.Add("Amount", GetType(String))
CommittedTable.Columns.Add("Initials", GetType(String))
For i As Integer = 0 To GridView1.Rows.Count
Dim tableRow As DataRow = CommittedTable.NewRow()
tableRow("Date Posted") = Date.Today
tableRow("Vendor") = ""
tableRow("Expense Description") = ""
tableRow("Ledger") = ""
tableRow("Amount") = ""
tableRow("Initials") = ""
CommittedTable.Rows.Add(tableRow)
Next
Session("CommsTable") = CommittedTable
BindDataComm()
End If
Lastly this is the RowDataBound event handler code:
Dim ddl As DropDownList = DirectCast(e.Row.FindControl("DropDownList1"), DropDownList)
If ddl Is Nothing Then
result = dbConnect(dbType.SqlServer, ConfigurationManager.AppSettings("SQLServerConnection"))
If result = "Successful" Then
dt = FillDataTable(dbType.SqlServer, "SELECT V_VendorNo + ' | ' + V_VendorName FROM VendorTbl")
ddl.DataSource = dt 'it errors out here'
ddl.DataTextField = "V_VendorNo"
ddl.DataValueField = "V_VendorName"
ddl.DataBind()
End If
End If
Are you checking the RowType for Datarow before binding the Data in RowDatabound event if not Please check it....

Resources