Hi nauna,
Check this sample. now take its reference.
HTML
<asp:TextBox runat="server" ID="txtCategoryID" AutoPostBack="true" OnTextChanged="OnChanged" />
Code
C#
protected void OnChanged(object sender, EventArgs e)
{
Data result = GetParent(GetData(), txtCategoryID.Text);
if (result != null)
{
while (result.Parent_Category_Id != "NULL")
{
if (result.Parent_Category_Id == result.Category_ID)
{
result = GetParent(GetData(), result.Parent_Category_Id);
break;
}
else
{
result = GetParent(GetData(), result.Parent_Category_Id);
}
}
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('" + result.Category_Name + "')", true);
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('CategoryID does not Exist!')", true);
}
}
private Data GetParent(List<Data> data, string parentId)
{
return data.Where(x => x.Category_ID == parentId).FirstOrDefault();
}
private List<Data> GetData()
{
List<Data> dt = new List<Data>();
dt.Add(new Data { Category_ID = "1", Category_Name = "Animal", Parent_Category_Id = "NULL" });
dt.Add(new Data { Category_ID = "2", Category_Name = "Vegetable", Parent_Category_Id = "NULL" });
dt.Add(new Data { Category_ID = "3", Category_Name = "Fruit", Parent_Category_Id = "NULL" });
dt.Add(new Data { Category_ID = "4", Category_Name = "Doggie", Parent_Category_Id = "1" });
dt.Add(new Data { Category_ID = "5", Category_Name = "Horsie", Parent_Category_Id = "1" });
dt.Add(new Data { Category_ID = "6", Category_Name = "Birdie", Parent_Category_Id = "1" });
dt.Add(new Data { Category_ID = "7", Category_Name = "Carrot", Parent_Category_Id = "2" });
dt.Add(new Data { Category_ID = "8", Category_Name = "Leafy", Parent_Category_Id = "2" });
dt.Add(new Data { Category_ID = "9", Category_Name = "Potato", Parent_Category_Id = "2" });
dt.Add(new Data { Category_ID = "10", Category_Name = "Celery", Parent_Category_Id = "10" });
dt.Add(new Data { Category_ID = "11", Category_Name = "Rutabaga", Parent_Category_Id = "2" });
dt.Add(new Data { Category_ID = "12", Category_Name = "Mango", Parent_Category_Id = "3" });
dt.Add(new Data { Category_ID = "13", Category_Name = "Orange", Parent_Category_Id = "3" });
dt.Add(new Data { Category_ID = "14", Category_Name = "Graps", Parent_Category_Id = "3" });
dt.Add(new Data { Category_ID = "15", Category_Name = "Apple", Parent_Category_Id = "3" });
dt.Add(new Data { Category_ID = "16", Category_Name = "Companion", Parent_Category_Id = "4" });
dt.Add(new Data { Category_ID = "17", Category_Name = "Herding", Parent_Category_Id = "4" });
dt.Add(new Data { Category_ID = "18", Category_Name = "Poodle", Parent_Category_Id = "19" });
dt.Add(new Data { Category_ID = "19", Category_Name = "Chihuahua", Parent_Category_Id = "19" });
dt.Add(new Data { Category_ID = "20", Category_Name = "Shepherd", Parent_Category_Id = "20" });
dt.Add(new Data { Category_ID = "21", Category_Name = "Collie", Parent_Category_Id = "20" });
return dt;
}
public class Data
{
public string Category_ID { get; set; }
public string Category_Name { get; set; }
public string Parent_Category_Id { get; set; }
}
VB.Net
Protected Sub OnChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim result As Data = GetParent(GetData(), txtCategoryID.Text)
If result IsNot Nothing Then
While result.Parent_Category_Id <> "NULL"
If result.Parent_Category_Id = result.Category_ID Then
result = GetParent(GetData(), result.Parent_Category_Id)
Exit While
Else
result = GetParent(GetData(), result.Parent_Category_Id)
End If
End While
ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('" & result.Category_Name & "')", True)
Else
ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('CategoryID does not Exist!')", True)
End If
End Sub
Private Function GetParent(ByVal data As List(Of Data), ByVal parentId As String) As Data
Return data.Where(Function(x) x.Category_ID = parentId).FirstOrDefault()
End Function
Private Function GetData() As List(Of Data)
Dim dt As List(Of Data) = New List(Of Data)()
dt.Add(New Data With {.Category_ID = "1", .Category_Name = "Animal", .Parent_Category_Id = "NULL"})
dt.Add(New Data With {.Category_ID = "2", .Category_Name = "Vegetable", .Parent_Category_Id = "NULL"})
dt.Add(New Data With {.Category_ID = "3", .Category_Name = "Fruit", .Parent_Category_Id = "NULL"})
dt.Add(New Data With {.Category_ID = "4", .Category_Name = "Doggie", .Parent_Category_Id = "1"})
dt.Add(New Data With {.Category_ID = "5", .Category_Name = "Horsie", .Parent_Category_Id = "1"})
dt.Add(New Data With {.Category_ID = "6", .Category_Name = "Birdie", .Parent_Category_Id = "1"})
dt.Add(New Data With {.Category_ID = "7", .Category_Name = "Carrot", .Parent_Category_Id = "2"})
dt.Add(New Data With {.Category_ID = "8", .Category_Name = "Leafy", .Parent_Category_Id = "2"})
dt.Add(New Data With {.Category_ID = "9", .Category_Name = "Potato", .Parent_Category_Id = "2"})
dt.Add(New Data With {.Category_ID = "10", .Category_Name = "Celery", .Parent_Category_Id = "10"})
dt.Add(New Data With {.Category_ID = "11", .Category_Name = "Rutabaga", .Parent_Category_Id = "2"})
dt.Add(New Data With {.Category_ID = "12", .Category_Name = "Mango", .Parent_Category_Id = "3"})
dt.Add(New Data With {.Category_ID = "13", .Category_Name = "Orange", .Parent_Category_Id = "3"})
dt.Add(New Data With {.Category_ID = "14", .Category_Name = "Graps", .Parent_Category_Id = "3"})
dt.Add(New Data With {.Category_ID = "15", .Category_Name = "Apple", .Parent_Category_Id = "3"})
dt.Add(New Data With {.Category_ID = "16", .Category_Name = "Companion", .Parent_Category_Id = "4"})
dt.Add(New Data With {.Category_ID = "17", .Category_Name = "Herding", .Parent_Category_Id = "4"})
dt.Add(New Data With {.Category_ID = "18", .Category_Name = "Poodle", .Parent_Category_Id = "19"})
dt.Add(New Data With {.Category_ID = "19", .Category_Name = "Chihuahua", .Parent_Category_Id = "19"})
dt.Add(New Data With {.Category_ID = "20", .Category_Name = "Shepherd", .Parent_Category_Id = "20"})
dt.Add(New Data With {.Category_ID = "21", .Category_Name = "Collie", .Parent_Category_Id = "20"})
Return dt
End Function
Public Class Data
Public Property Category_ID As String
Public Property Category_Name As String
Public Property Parent_Category_Id As String
End Class
Screenshot