data is retrieved from the attendance table and the column name is status I want to change the status of the employee to 'leave' if the status of employee is other than 'present' and 'absent'
MY aspx design code
<asp:DropDownList ID="ddlStatus" runat="server" DataValueField="<%# statusConversion(Eval("Status")) %>">
<asp:ListItem value="Present" Text = "Present"></asp:ListItem>
<asp:ListItem value="Absent" Text="Absent"></asp:ListItem>
<asp:ListItem value="Leave" Text="Leave"></asp:ListItem>
</asp:DropDownList>ode here
MY C# backend function code
public String statusConversion(object myVal)
{
String p = "Present";
String a = "Absent";
String l = "Leave";
String val = myVal.ToString();
if (val.Equals(a) || val.Equals(p))
{
return val;
}
else
{
val = l;
return val;
}
}
According to your description, I suggest you could use gridview row databound method to achieve your requirement. You could set the dropdown’s select value in this method and use your custom method to check the stauts codes.
More details, you could refer to below codes:
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridviewWithDDL.aspx.cs" Inherits="WebFormIdentityTest.GridviewWithDDL" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="false" OnDataBound="GridView1_DataBound" >
<Columns>
<asp:BoundField DataField="id" HeaderText="Id" ItemStyle-Width="30" />
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Operation">
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("Status") %>'/>
<asp:DropDownList ID="ddlStatus" runat="server" >
<asp:ListItem value="Present" Text = "Present"></asp:ListItem>
<asp:ListItem value="Absent" Text="Absent"></asp:ListItem>
<asp:ListItem value="Leave" Text="Leave"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Code-behind:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebFormIdentityTest
{
public partial class GridviewWithDDL : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
DataTable d1 = new DataTable();
d1.Columns.Add("id");
d1.Columns.Add("Status");
d1.Columns.Add("SelectSource");
d1.Columns.Add("SourceName");
d1.Rows.Add("1", "Present", "bbbb", "aaaaaaa");
d1.Rows.Add("2", "Absent", "ccccc", "dddddd");
d1.Rows.Add("3", "Leave", "dddd", "fffff");
d1.Rows.Add("4", "aaaa", "eeee", "ffff");
d1.Rows.Add("5", "Leave", "cccc", "asdasdas");
d1.Rows.Add("6", "bbb", "werwer", "qweqwe");
GridView1.DataSource = d1;
GridView1.DataBind();
}
public static String statusConversion(object myVal)
{
String p = "Present";
String a = "Absent";
String l = "Leave";
String val = myVal.ToString();
if (val.Equals(a) || val.Equals(p))
{
return val;
}
else
{
val = l;
return val;
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow gvRow in GridView1.Rows)
{
if (gvRow.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = gvRow.FindControl("ddlStatus") as DropDownList;
HiddenField hf = gvRow.FindControl("HiddenField1") as HiddenField;
ddl.SelectedValue = statusConversion(hf.Value);
}
}
}
}
}