hello,
thank for your site and your help.
if i bind dropdownlist by datatable it loose the new value on update.
I want just to catch the value of the dropdownlist after editing for update my datatable but there is a problem, a bad postback or onchangeindex.
i don t understand.
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
MsgBox("load")
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
ViewState("dt") = dt
Me.BindGrid()
End If
End Sub
Protected Sub BindGrid()
GridView1.DataSource = TryCast(ViewState("dt"), DataTable)
GridView1.DataBind()
End Sub
Protected Sub OnRowEditing(sender As Object, e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
Me.BindGrid()
End Sub
Protected Sub OnUpdate(sender As Object, e As EventArgs)
MsgBox("update")
Dim row As GridViewRow = TryCast(TryCast(sender, LinkButton).NamingContainer, GridViewRow)
Dim name As String = TryCast(row.FindControl("txtName"), TextBox).Text
MsgBox(name & " up ")
Dim country As DropDownList = TryCast(sender.FindControl("ddlCountries"), DropDownList)
country.Items.FindByText(country.SelectedItem.Text).Selected = True
Dim dt As DataTable = TryCast(ViewState("dt"), DataTable)
dt.Rows(row.RowIndex)("Name") = name
dt.Rows(row.RowIndex)("Country") = country.SelectedItem.Text
ViewState("dt") = dt
GridView1.EditIndex = -1
Me.BindGrid()
End Sub
Protected Sub OnCancel(sender As Object, e As EventArgs)
GridView1.EditIndex = -1
Me.BindGrid()
End Sub
Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
MsgBox("bound")
If e.Row.RowType = DataControlRowType.DataRow AndAlso GridView1.EditIndex = e.Row.RowIndex Then
Dim txtName As TextBox = TryCast(e.Row.FindControl("txtName"), TextBox)
Dim country As String = TryCast(e.Row.DataItem, DataRowView)("Country").ToString()
MsgBox(country & " poi ")
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Cours"), New DataColumn("Description")})
Dim csvPath As String = System.Web.HttpContext.Current.Server.MapPath("TextFile.txt")
Dim csvData As String = File.ReadAllText(csvPath)
Dim w As Integer = 0
For Each row1 As String In csvData.Split(ControlChars.Lf)
If Not String.IsNullOrEmpty(row1) Then
dt.Rows.Add()
Dim i As Integer = 0
For Each cell As String In row1.Split(";"c)
dt.Rows(dt.Rows.Count - 1)(i) = cell
i += 1
Next
MsgBox(dt.Rows(dt.Rows.Count - 1)(i - 2) & " " & dt.Rows(dt.Rows.Count - 1)(i - 1))
w += 1
End If
Next
Dim ddlCountries As DropDownList = TryCast(e.Row.FindControl("ddlCountries"), DropDownList)
ddlCountries.DataSource = dt
ddlCountries.DataTextField = "Cours"
ddlCountries.DataValueField = "Description"
ddlCountries.DataBind()
ddlCountries.Items.Insert(0, New ListItem("Please select", ""))
'Select the Country of Customer in DropDownList
ddlCountries.Items.FindByText(country).Selected = True
'ddlCountries.Attributes.Add("onmouseover", "this.title=this.options[this.selectedIndex].value")
End If
End Sub
End Class
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowEditing="OnRowEditing" onrowdatabound="OnRowDataBound">
<Columns>
<asp:TemplateField HeaderText="Name" ItemStyle-Width="150">
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country" ItemStyle-Width="150">
<ItemTemplate>
<%# Eval("Country") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlCountries" runat="server" AutoPostBack="true" OnSelectedIndexChanged ="ItemSelected" >
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="Edit" runat="server" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton Text="Update" runat="server" OnClick="OnUpdate" OnClientClick="return Validate(this)" />
<asp:LinkButton Text="Cancel" runat="server" OnClick="OnCancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and my text file :
United States;United States
India;India
France;France
Russia;Russia