Answered by:
Listview with dropdown and doing edit delete

Question
-
User1126057398 posted
What's the best way to display dropdown in itemtemplate and doing CRUD operation?
Thursday, October 3, 2019 3:00 PM
Answers
-
User288213138 posted
Hi geetasks,
What's the best way to display dropdown in itemtemplate and doing CRUD operation?
I don't think there is a best way, only the best way for you, this depends on your own requirement.
and I made demo for your as a reference:
<asp:ListView runat="server" ID="ListView1" ItemPlaceholderID="itemPlaceHolder1" OnItemEditing="EditRecord" OnItemCanceling="CancelEditRecord" DataKeyNames="AutoId" OnItemInserting="InsertRecord" OnItemUpdating="UpdateRecord" InsertItemPosition="LastItem" OnItemDeleting="DeleteRecord"> <LayoutTemplate> <table border="1"> <tr> <th>AutoId</th> <th>First Name</th> <th>Last Name</th> <th>Age</th> <th>Activity</th> <th>Drop</th> <th>Button</th> </tr> <asp:PlaceHolder ID="itemPlaceHolder1" runat="server"></asp:PlaceHolder> </table> </LayoutTemplate> <ItemTemplate> <tr> <td><%# Eval("AutoID") %></td> <td><%# Eval("FirstName") %></td> <td><%# Eval("LastName") %></td> <td><%#Eval("Age") %></td> <td><%# Eval("Active") %></td> <td> <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem>1</asp:ListItem> <asp:ListItem>2</asp:ListItem> <asp:ListItem>3</asp:ListItem> </asp:DropDownList></td> <td> <asp:LinkButton ID="lnkDel" runat="server" Text="Delete" CommandName="Delete" /> <asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit" /> </td> </tr> </ItemTemplate> <EditItemTemplate> <tr> <td><asp:Label ID="Label1" runat="server" Text='<%# Eval("AutoId") %>'></asp:Label></td> <td><asp:TextBox ID="txtEFName" runat="server" Text='<%# Eval("FirstName") %>' /></td> <td><asp:TextBox ID="txtELName" runat="server" Text='<%#Eval("LastName") %>' /></td> <td><asp:TextBox ID="txtEAge" runat="server" Text='<%# Eval("Age") %>' /></td> <td><asp:TextBox ID="dropEACtive" runat="server" Text='<%# Eval("Active") %>' /></td> <td><asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem>1</asp:ListItem> <asp:ListItem>2</asp:ListItem> <asp:ListItem>3</asp:ListItem> </asp:DropDownList></td> <td><asp:LinkButton ID="lnkUpdate" runat="server" Text="Update" CommandName="Update" /> <asp:LinkButton ID="lnkCancel" runat="server" Text="Cancel" CommandName="Cancel" /></td> </tr> </EditItemTemplate> <InsertItemTemplate> <tr> <td><asp:TextBox ID="txtId" runat="server" /></td> <td><asp:TextBox ID="txtFName" runat="server" /></td> <td><asp:TextBox ID="txtLName" runat="server" /> </td> <td><asp:TextBox ID="txtAge" runat="server" /></td> <td><asp:TextBox ID="txtActivity" runat="server" /></td> <td> <asp:LinkButton ID="lnkInser" runat="server" Text="Insert" CommandName="Insert" /> </td> </tr> </InsertItemTemplate> </asp:ListView> string _connStr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { PopulateData(); } } private void PopulateData() { DataTable table = new DataTable(); using (SqlConnection conn = new SqlConnection(_connStr)) { string sql = "Select * from PersonalDetail"; using (SqlCommand cmd = new SqlCommand(sql, conn)) { using (SqlDataAdapter ad = new SqlDataAdapter(cmd)) { ad.Fill(table); } } } ListView1.DataSource = table; ListView1.DataBind(); } protected void InsertRecord(object sender, ListViewInsertEventArgs e) { ListViewItem item = e.Item; TextBox ti = (TextBox)item.FindControl("txtId"); TextBox tF = (TextBox)item.FindControl("txtFName"); TextBox tL = (TextBox)item.FindControl("txtLName"); TextBox tA = (TextBox)item.FindControl("txtAge"); TextBox EA = (TextBox)item.FindControl("txtActivity"); using (SqlConnection conn = new SqlConnection(_connStr)) { string sql = "Insert Into PersonalDetail (AutoId,FirstName, LastName, Age, Active) VALUES (" + "@AutoId, @FirstName, @LastName, @Age, @Active)"; using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddWithValue("@AutoId", ti.Text.Trim()); cmd.Parameters.AddWithValue("@FirstName", tF.Text.Trim()); cmd.Parameters.AddWithValue("@LastName", tL.Text.Trim()); cmd.Parameters.AddWithValue("@Age", tA.Text.Trim()); cmd.Parameters.AddWithValue("@Active", EA.Text.Trim()); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } PopulateData(); } protected void EditRecord(object sender, ListViewEditEventArgs e) { ListView1.EditIndex = e.NewEditIndex; PopulateData(); } protected void UpdateRecord(object sender, ListViewUpdateEventArgs e) { int autoId = int.Parse(ListView1.DataKeys[e.ItemIndex].Value.ToString()); ListViewItem item = ListView1.Items[e.ItemIndex]; TextBox tF = (TextBox)item.FindControl("txtEFName"); TextBox tL = (TextBox)item.FindControl("txtELName"); TextBox tA = (TextBox)item.FindControl("txtEAge"); TextBox dropEA = (TextBox)item.FindControl("dropEActive"); using (SqlConnection conn = new SqlConnection(_connStr)) { string sql = "Update PersonalDetail set FirstName = @FirstName, LastName=@LastName, Age= @Age, Active = @Active" + " where AutoId = @AutoId"; using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddWithValue("@FirstName", tF.Text.Trim()); cmd.Parameters.AddWithValue("@LastName", tL.Text.Trim()); cmd.Parameters.AddWithValue("@Age", tA.Text.Trim()); cmd.Parameters.AddWithValue("@Active", dropEA.Text.Trim()); cmd.Parameters.AddWithValue("@AutoId", autoId); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } ListView1.EditIndex = -1; PopulateData(); } protected void CancelEditRecord(object sender, ListViewCancelEventArgs e) { ListView1.EditIndex = -1; PopulateData(); } protected void DeleteRecord(object sender, ListViewDeleteEventArgs e) { var autoid = ListView1.DataKeys[e.ItemIndex].Value.ToString(); using (SqlConnection conn = new SqlConnection(_connStr)) { string sql = "Delete from PersonalDetail " + " where AutoId = @AutoId"; using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddWithValue("@AutoId", autoid); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } PopulateData(); }
The result:
Best regards,
Sam
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 4, 2019 7:37 AM -
User1126057398 posted
Yes. Still It was not firing, so I had applied logic on ItemDataBound event and using Jquery. Now, the only prob. is that dropdown changed value is displayed on 2nd postback?
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, October 15, 2019 6:49 AM -
User1126057398 posted
Solved the problem by calling dropdown SelectedIndex changed method forcefully. In Jquery, I had set a variable IsDDClicked=1 and in ListView Bind method, checked if Dropdown is clicked ie if IsDDClicked=1, then fired dropdown Selected index like: dlFndEqu_SelectedIndexChanged(null, null);
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, October 15, 2019 11:24 AM
All replies
-
User288213138 posted
Hi geetasks,
What's the best way to display dropdown in itemtemplate and doing CRUD operation?
I don't think there is a best way, only the best way for you, this depends on your own requirement.
and I made demo for your as a reference:
<asp:ListView runat="server" ID="ListView1" ItemPlaceholderID="itemPlaceHolder1" OnItemEditing="EditRecord" OnItemCanceling="CancelEditRecord" DataKeyNames="AutoId" OnItemInserting="InsertRecord" OnItemUpdating="UpdateRecord" InsertItemPosition="LastItem" OnItemDeleting="DeleteRecord"> <LayoutTemplate> <table border="1"> <tr> <th>AutoId</th> <th>First Name</th> <th>Last Name</th> <th>Age</th> <th>Activity</th> <th>Drop</th> <th>Button</th> </tr> <asp:PlaceHolder ID="itemPlaceHolder1" runat="server"></asp:PlaceHolder> </table> </LayoutTemplate> <ItemTemplate> <tr> <td><%# Eval("AutoID") %></td> <td><%# Eval("FirstName") %></td> <td><%# Eval("LastName") %></td> <td><%#Eval("Age") %></td> <td><%# Eval("Active") %></td> <td> <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem>1</asp:ListItem> <asp:ListItem>2</asp:ListItem> <asp:ListItem>3</asp:ListItem> </asp:DropDownList></td> <td> <asp:LinkButton ID="lnkDel" runat="server" Text="Delete" CommandName="Delete" /> <asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit" /> </td> </tr> </ItemTemplate> <EditItemTemplate> <tr> <td><asp:Label ID="Label1" runat="server" Text='<%# Eval("AutoId") %>'></asp:Label></td> <td><asp:TextBox ID="txtEFName" runat="server" Text='<%# Eval("FirstName") %>' /></td> <td><asp:TextBox ID="txtELName" runat="server" Text='<%#Eval("LastName") %>' /></td> <td><asp:TextBox ID="txtEAge" runat="server" Text='<%# Eval("Age") %>' /></td> <td><asp:TextBox ID="dropEACtive" runat="server" Text='<%# Eval("Active") %>' /></td> <td><asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem>1</asp:ListItem> <asp:ListItem>2</asp:ListItem> <asp:ListItem>3</asp:ListItem> </asp:DropDownList></td> <td><asp:LinkButton ID="lnkUpdate" runat="server" Text="Update" CommandName="Update" /> <asp:LinkButton ID="lnkCancel" runat="server" Text="Cancel" CommandName="Cancel" /></td> </tr> </EditItemTemplate> <InsertItemTemplate> <tr> <td><asp:TextBox ID="txtId" runat="server" /></td> <td><asp:TextBox ID="txtFName" runat="server" /></td> <td><asp:TextBox ID="txtLName" runat="server" /> </td> <td><asp:TextBox ID="txtAge" runat="server" /></td> <td><asp:TextBox ID="txtActivity" runat="server" /></td> <td> <asp:LinkButton ID="lnkInser" runat="server" Text="Insert" CommandName="Insert" /> </td> </tr> </InsertItemTemplate> </asp:ListView> string _connStr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { PopulateData(); } } private void PopulateData() { DataTable table = new DataTable(); using (SqlConnection conn = new SqlConnection(_connStr)) { string sql = "Select * from PersonalDetail"; using (SqlCommand cmd = new SqlCommand(sql, conn)) { using (SqlDataAdapter ad = new SqlDataAdapter(cmd)) { ad.Fill(table); } } } ListView1.DataSource = table; ListView1.DataBind(); } protected void InsertRecord(object sender, ListViewInsertEventArgs e) { ListViewItem item = e.Item; TextBox ti = (TextBox)item.FindControl("txtId"); TextBox tF = (TextBox)item.FindControl("txtFName"); TextBox tL = (TextBox)item.FindControl("txtLName"); TextBox tA = (TextBox)item.FindControl("txtAge"); TextBox EA = (TextBox)item.FindControl("txtActivity"); using (SqlConnection conn = new SqlConnection(_connStr)) { string sql = "Insert Into PersonalDetail (AutoId,FirstName, LastName, Age, Active) VALUES (" + "@AutoId, @FirstName, @LastName, @Age, @Active)"; using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddWithValue("@AutoId", ti.Text.Trim()); cmd.Parameters.AddWithValue("@FirstName", tF.Text.Trim()); cmd.Parameters.AddWithValue("@LastName", tL.Text.Trim()); cmd.Parameters.AddWithValue("@Age", tA.Text.Trim()); cmd.Parameters.AddWithValue("@Active", EA.Text.Trim()); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } PopulateData(); } protected void EditRecord(object sender, ListViewEditEventArgs e) { ListView1.EditIndex = e.NewEditIndex; PopulateData(); } protected void UpdateRecord(object sender, ListViewUpdateEventArgs e) { int autoId = int.Parse(ListView1.DataKeys[e.ItemIndex].Value.ToString()); ListViewItem item = ListView1.Items[e.ItemIndex]; TextBox tF = (TextBox)item.FindControl("txtEFName"); TextBox tL = (TextBox)item.FindControl("txtELName"); TextBox tA = (TextBox)item.FindControl("txtEAge"); TextBox dropEA = (TextBox)item.FindControl("dropEActive"); using (SqlConnection conn = new SqlConnection(_connStr)) { string sql = "Update PersonalDetail set FirstName = @FirstName, LastName=@LastName, Age= @Age, Active = @Active" + " where AutoId = @AutoId"; using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddWithValue("@FirstName", tF.Text.Trim()); cmd.Parameters.AddWithValue("@LastName", tL.Text.Trim()); cmd.Parameters.AddWithValue("@Age", tA.Text.Trim()); cmd.Parameters.AddWithValue("@Active", dropEA.Text.Trim()); cmd.Parameters.AddWithValue("@AutoId", autoId); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } ListView1.EditIndex = -1; PopulateData(); } protected void CancelEditRecord(object sender, ListViewCancelEventArgs e) { ListView1.EditIndex = -1; PopulateData(); } protected void DeleteRecord(object sender, ListViewDeleteEventArgs e) { var autoid = ListView1.DataKeys[e.ItemIndex].Value.ToString(); using (SqlConnection conn = new SqlConnection(_connStr)) { string sql = "Delete from PersonalDetail " + " where AutoId = @AutoId"; using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddWithValue("@AutoId", autoid); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } PopulateData(); }
The result:
Best regards,
Sam
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 4, 2019 7:37 AM -
User1126057398 posted
Thanks. But the problem is that dropdown SelectedIndexChanged is not firing.
I had created UserControl. Under that listview is placed. Inside ListView Item Template, I had defined DropdownList. Wondering What can be the reason that dropdown SelectedIndexChanged is not firing?
Monday, October 7, 2019 9:43 AM -
User288213138 posted
Hi geetaska,
But the problem is that dropdown SelectedIndexChanged is not firing.Do you set AutoPostBack="true"?
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> <asp:ListItem>1</asp:ListItem> <asp:ListItem>2</asp:ListItem> <asp:ListItem>3</asp:ListItem> </asp:DropDownList>
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write("ddd");
}The result:
Best regards,
Sam
Tuesday, October 8, 2019 10:25 AM -
User1126057398 posted
Yes. Still It was not firing, so I had applied logic on ItemDataBound event and using Jquery. Now, the only prob. is that dropdown changed value is displayed on 2nd postback?
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, October 15, 2019 6:49 AM -
User288213138 posted
Hi geetasks,
Yes. Still It was not firing, so I had applied logic on ItemDataBound event and using Jquery. Now, the only prob. is that dropdown changed value is displayed on 2nd postback?According to your description, i can't reproduce your question.
I need detailed code to reproduce your problem, So please post your aspx and aspx.cs code.
and this is new question, please post your question in new thread.
Best regards,
Sam
Tuesday, October 15, 2019 7:51 AM -
User1126057398 posted
Solved the problem by calling dropdown SelectedIndex changed method forcefully. In Jquery, I had set a variable IsDDClicked=1 and in ListView Bind method, checked if Dropdown is clicked ie if IsDDClicked=1, then fired dropdown Selected index like: dlFndEqu_SelectedIndexChanged(null, null);
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, October 15, 2019 11:24 AM