Hi Makumbi,
First you need to find tr (TableRow) and then reference the TextBox using the row and set DropDownList selected value inside TextBox.
Please refer below sample.
Note: For this sample i have used temporary DataTable. For more details refer How to create Temporary Table in ASP.Net using C# and VB.Net.
HTML
<asp:GridView ID="gvCustomers" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Customer Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<select id="ddlCountries">
<option value="">Please select</option>
<option value="United States">United States</option>
<option value="India">India</option>
<option value="France">France</option>
<option value="Russia">Russia</option>
</select>
<input id="txtCountry" type="text" readonly="readonly" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('[id*=ddlCountries]').live('change', function () {
var tr = $(this).closest('tr');
var country = $(this).find('option:selected').text();
$(tr).find('[id*=txtCountry]').val(country);
});
});
</script>
Namespace
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("CustomerId",typeof(int)),
new DataColumn("Name", typeof(string))
});
dt.Rows.Add(1, "John Hammond");
dt.Rows.Add(2, "Mudassar Khan");
dt.Rows.Add(3, "Suzanne Mathews");
dt.Rows.Add(4, "Robert Schidner");
this.gvCustomers.DataSource = dt;
this.gvCustomers.DataBind();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("CustomerId", GetType(Integer)),
New DataColumn("Name", GetType(String))
})
dt.Rows.Add(1, "John Hammond")
dt.Rows.Add(2, "Mudassar Khan")
dt.Rows.Add(3, "Suzanne Mathews")
dt.Rows.Add(4, "Robert Schidner")
Me.gvCustomers.DataSource = dt
Me.gvCustomers.DataBind()
End If
End Sub
Screenshot