List as DataSource for DataList - datasource

All Types implement IEnumerable interface could be used for a DataSource of a DataList. For example List. But what we will write for the data bound expression in ItemTemplate?
List<int> myList = new List<int>();
for(int i=0; i<10; i++)
myList.Add(i);
myDataList.DataSource = myList;
...
<ItemTemplate>
<asp:TextBox ID="myTextBox" runat="server" Text='<%# Bind(???) %>' />
</ItemTemplate>

Try this:
<%# Container.DataItem.ToString() %>

Related

Recreating Form in PHP from ASP, Multiple results with dynamically generated drop downs?

I'm recreating a multi-Part form, but this question pertains specifically to a page where someone signing up can mark what (if any) commodities they may have or grow. You can see in my ASPX and ASPX.CS , how we have accomplished it thus far (mainly utilizing classes and some ajax/javascript). Now that we are recreating the system in PHP, I'm mulling over better (maybe simpler is a better word) ways to do this. Currently, the list of commodities is pulled from a SQL table, and a drop down is displayed next to each one (depending on it's unit of measurement - EX: Acre, Flock, Head, etc). After the joining member selects his crops, they are committed to a different table with his/her member ID and quantity of said commodity. See Below:
Question:
What, if there is one, is a better way to do this in PHP?
Thanks for your time =)
commodities.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="commodities.aspx.cs" Inherits="Secure_commodities" MasterPageFile="~/secure/joinfb.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="content" Runat="Server">
<style type="text/css">
collapsePanel {
background-color:white;
overflow:hidden;
}
collapsePanelHeader{
width:100%;
height:30px;
border-color: Red;
color:#FFF;
font-weight:bold;
}
.panelImage{
vertical-align:baseline;
}
</style>
<ajaxToolkit:ToolkitScriptManager ID="tk1" runat="server" EnablePartialRendering="true" />
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<p class="formHeading"><b>Member Commodities</b></p>
<p>
<label>
Do you include any of the following production practices in your operation?
</label>
</p>
<table width="50%" cellpadding="2" cellspacing="2">
<tr>
<td>
<asp:CheckBox ID="chkOCC" runat="server" Text="Organic Crop Certification" />
</td>
<td>
<asp:CheckBox ID="chkUSDA" runat="server" Text="USDA Conservation Program" />
</td>
</tr>
</table>
<br/>
<p class="formHeading">
<asp:Panel ID="pnl1" runat="server">
<asp:Image ID="img1" runat="server" CssClass="panelImage" /> <asp:LinkButton ID="lnk1" runat="server" Text="Dairy/Livestock/Poultry" Font-Size="Medium" /> <asp:Label ID="lbl1" runat="server" />
</asp:Panel>
<%--<asp:ImageButton ID="Image1" runat="server" ImageUrl="~/images/bg-menu-main.png" AlternateText="(Show Details...)"/> --%>
<%--<asp:Label ID="Label1" runat="server">(Show Details...)</asp:Label></p>--%>
<asp:Panel ID="Panel1" runat="server" CssClass="collapsePanel" Height="0">
<asp:datalist id="dtlList1" DataKeyField="CMDY_Code" Width="100%" RepeatColumns="3" runat="server" OnItemDataBound="Bind_Unit_1"
CellPadding="1" CellSpacing="1" >
<ItemTemplate>
<table width="100%">
<tr>
<td style="width: auto" align="left" >
<asp:CheckBox Runat="server" ID="chk1" AutoPostBack="true"/>
</td>
<td style="width: 45%" align="left">
<asp:Label ID="lblTitle1" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Commodity") %>' />
</td>
<td style="width: 50%" align="left">
<asp:DropDownList ID="ddl1" Runat="server" CssClass="input" AutoPostBack="true" OnSelectedIndexChanged="dlQuantityChanged1" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:datalist>
</asp:Panel>
<ajaxToolkit:CollapsiblePanelExtender ID="CollapsiblePanelExtender0" runat="Server"
TargetControlID="Panel1"
ExpandControlID="pnl1"
CollapseControlID="pnl1"
Collapsed="True"
SuppressPostBack="false"
ImageControlID="img1" CollapsedImage="http://test.com/images/expand_blue.jpg" ExpandedImage="../images/collapse_blue.jpg"
TextLabelID="lbl1" CollapsedText=" " ExpandedText=" "
/>
<p class="formHeading">
<asp:Panel ID="pnl2" runat="server">
<asp:Image ID="img2" runat="server" CssClass="panelImage" /> <asp:LinkButton ID="lnk2" runat="server" Text="Field Crops" Font-Size="Medium" /> <asp:Label ID="lbl2" runat="server" />
</asp:Panel>
<asp:Panel ID="Panel2" runat="server" CssClass="collapsePanel" Height="0">
<asp:datalist id="dtlList2" DataKeyField="CMDY_Code" Width="100%" RepeatColumns="3" runat="server" OnItemDataBound="Bind_Unit_2"
CellPadding="1" CellSpacing="1" >
<ItemTemplate>
<table width="100%">
<tr>
<td style="width: auto" align="left" >
<asp:CheckBox Runat="server" ID="chk1" AutoPostBack="true"/>
</td>
<td style="width: 45%" align="left">
<asp:Label ID="lblTitle1" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Commodity") %>' />
</td>
<td style="width: 50%" align="left">
<asp:DropDownList ID="ddl1" Runat="server" CssClass="input" AutoPostBack="true" OnSelectedIndexChanged="dlQuantityChanged2" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:datalist>
</asp:Panel>
<ajaxToolkit:CollapsiblePanelExtender ID="CollapsiblePanelExtender1" runat="Server"
TargetControlID="Panel2"
ExpandControlID="pnl2"
CollapseControlID="pnl2"
Collapsed="True"
SuppressPostBack="false"
ImageControlID="img2" CollapsedImage="http://test.com/images/expand_blue.jpg" ExpandedImage="../images/collapse_blue.jpg"
TextLabelID="lbl2" CollapsedText=" " ExpandedText=" "
/>
<p class="formHeading">
<asp:Panel ID="pnl3" runat="server">
<asp:Image ID="img3" runat="server" CssClass="panelImage"/> <asp:LinkButton ID="lnk3" runat="server" Text="Horticulture/Forestry" Font-Size="Medium" /> <asp:Label ID="lbl3" runat="server" />
</asp:Panel>
<asp:Panel ID="Panel3" runat="server" CssClass="collapsePanel" Height="0">
<asp:datalist id="dtlList3" DataKeyField="CMDY_Code" Width="100%" RepeatColumns="3" runat="server" OnItemDataBound="Bind_Unit_3"
CellPadding="1" CellSpacing="1" >
<ItemTemplate>
<table width="100%">
<tr>
<td style="width: auto" align="left" >
<asp:CheckBox Runat="server" ID="chk1" AutoPostBack="true" />
</td>
<td style="width: 45%" align="left">
<asp:Label ID="lblTitle1" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Commodity") %>' />
</td>
<td style="width: 50%" align="left">
<asp:DropDownList ID="ddl1" Runat="server" CssClass="input" AutoPostBack="true" OnSelectedIndexChanged="dlQuantityChanged3" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:datalist>
</asp:Panel>
<ajaxToolkit:CollapsiblePanelExtender ID="CollapsiblePanelExtender2" runat="Server"
TargetControlID="Panel3"
ExpandControlID="pnl3"
CollapseControlID="pnl3"
Collapsed="True"
SuppressPostBack="false"
ImageControlID="img3" CollapsedImage="http://test.com/images/expand_blue.jpg" ExpandedImage="../images/collapse_blue.jpg"
TextLabelID="lbl3" CollapsedText=" " ExpandedText=" "
/>
<p class="formHeading">
<asp:Panel ID="pnl4" runat="server">
<asp:Image ID="img4" runat="server" CssClass="panelImage"/> <asp:LinkButton ID="lnk4" runat="server" Text="Trees/Vines" Font-Size="Medium" /> <asp:Label ID="lbl4" runat="server" />
</asp:Panel>
<asp:Panel ID="Panel4" runat="server" CssClass="collapsePanel" Height="0">
<asp:datalist id="dtlList4" DataKeyField="CMDY_Code" Width="100%" RepeatColumns="3" runat="server" OnItemDataBound="Bind_Unit_4"
CellPadding="1" CellSpacing="1" >
<ItemTemplate>
<table width="100%">
<tr>
<td style="width: auto" align="left" >
<asp:CheckBox Runat="server" ID="chk1" AutoPostBack="true" />
</td>
<td style="width: 45%" align="left">
<asp:Label ID="lblTitle1" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Commodity") %>' />
</td>
<td style="width: 50%" align="left">
<asp:DropDownList ID="ddl1" Runat="server" CssClass="input" AutoPostBack="true" OnSelectedIndexChanged="dlQuantityChanged4" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:datalist>
</asp:Panel>
<ajaxToolkit:CollapsiblePanelExtender ID="CollapsiblePanelExtender3" runat="Server"
TargetControlID="Panel4"
ExpandControlID="pnl4"
CollapseControlID="pnl4"
Collapsed="True"
SuppressPostBack="false"
ImageControlID="img4" CollapsedImage="http://test.com/images/expand_blue.jpg" ExpandedImage="../images/collapse_blue.jpg"
TextLabelID="lbl4" CollapsedText=" " ExpandedText=" "
/>
<p class="formHeading">
<asp:Panel ID="pnl5" runat="server">
<asp:Image ID="img5" runat="server" CssClass="panelImage"/> <asp:LinkButton ID="lnk5" runat="server" Text="Vegetables" Font-Size="Medium" /> <asp:Label ID="lbl5" runat="server" />
</asp:Panel>
<asp:Panel ID="Panel5" runat="server" CssClass="collapsePanel" Height="0">
<asp:datalist id="dtlList5" DataKeyField="CMDY_Code" Width="100%" RepeatColumns="3" runat="server" OnItemDataBound="Bind_Unit_5"
CellPadding="1" CellSpacing="1" >
<ItemTemplate>
<table width="100%">
<tr>
<td style="width: auto" align="left" >
<asp:CheckBox Runat="server" ID="chk1" AutoPostBack="true" />
</td>
<td style="width: 45%" align="left">
<asp:Label ID="lblTitle1" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Commodity") %>' />
</td>
<td style="width: 50%" align="left">
<asp:DropDownList ID="ddl1" Runat="server" CssClass="input" AutoPostBack="true" OnSelectedIndexChanged="dlQuantityChanged5" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:datalist>
</asp:Panel>
<ajaxToolkit:CollapsiblePanelExtender ID="CollapsiblePanelExtender4" runat="Server"
TargetControlID="Panel5"
ExpandControlID="pnl5"
CollapseControlID="pnl5"
Collapsed="True"
SuppressPostBack="false"
ImageControlID="img5" CollapsedImage="http://test.com/images/expand_blue.jpg" ExpandedImage="../images/collapse_blue.jpg"
TextLabelID="lbl5" CollapsedText=" " ExpandedText=" "
/>
<p>
<asp:Button ID="btnNext" runat="server" Text="Next" OnClick="btnNext_OnClick" />
</p>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
commodities.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using TESTReg;
public partial class Secure_commodities : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Load_DList();
if (Request.QueryString["Mode"] == "Edit")
{
// populate each CMDY
Membership Member = new Membership();
Member = (Membership)Session.Contents["Membership"];
Load_CMDY(dtlList1, Member, CollapsiblePanelExtender0);
Load_CMDY(dtlList2, Member, CollapsiblePanelExtender1);
Load_CMDY(dtlList3, Member, CollapsiblePanelExtender2);
Load_CMDY(dtlList4, Member, CollapsiblePanelExtender3);
Load_CMDY(dtlList5, Member, CollapsiblePanelExtender4);
//List<Commodity> list = new List<Commodity>();
//for (int i = 0; i < Member.Commodities.Count; i++)
//{
// list.Add(Member.Commodities[i]);
//}
//dlChildren.DataSource = list;
////dlChildren.DataBind();
//foreach (DataListItem li in dlChildren.Items)
//{
// TextBox tFname = (TextBox)(li.FindControl("txtChildFName"));
// TextBox tLname = (TextBox)(li.FindControl("txtChildLName"));
// TextBox tDOB = (TextBox)(li.FindControl("txtChildDOB"));
// tFname.Text = Member.Dependent[li.ItemIndex].FirstName;
// tLname.Text = Member.Dependent[li.ItemIndex].LastName;
// tDOB.Text = Member.Dependent[li.ItemIndex].DOB;
//}
//ddlNumChild.ClearSelection();
//ddlNumChild.Items.FindByValue(Member.Dependent.Count.ToString()).Selected = true;
}
}
}
private void Load_CMDY(DataList dl, Membership Member, AjaxControlToolkit.CollapsiblePanelExtender cp)
{
for (int i = 0; i < Member.Commodities.Count; i++)
{
foreach (DataListItem li in dl.Items)
{
CheckBox _chk = (CheckBox)(li.FindControl("chk1"));
DropDownList _ddl = (DropDownList)(li.FindControl("ddl1"));
if (Member.Commodities[i].Cmdy_Code == dl.DataKeys[li.ItemIndex].ToString())
{
_chk.Checked = Member.Commodities[i].SelectedItem;
_ddl.ClearSelection();
_ddl.Items.FindByValue(Member.Commodities[i].Unit_Value).Selected = true;
cp.Collapsed = false;
break;
}
}
}
}
private void Load_DList()
{
string sSQL;
sSQL = "SELECT CMDY_CODE, Commodity, Category FROM _WJ_TBL_Commodity WHERE Category='Dairy/Livestock/Poultry' ORDER BY Commodity";
System.Data.DataSet ds = new System.Data.DataSet();
ds = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.ConnectionStrings["JoinFB_Conn"].ConnectionString,
System.Data.CommandType.Text, sSQL);
dtlList1.DataSource = ds;
dtlList1.DataBind();
sSQL = "SELECT CMDY_CODE, Commodity, Category FROM _WJ_TBL_Commodity WHERE Category='Field Crops' ORDER BY Commodity";
ds = new System.Data.DataSet();
ds = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.ConnectionStrings["JoinFB_Conn"].ConnectionString,
System.Data.CommandType.Text, sSQL);
dtlList2.DataSource = ds;
dtlList2.DataBind();
sSQL = "SELECT CMDY_CODE, Commodity, Category FROM _WJ_TBL_Commodity WHERE Category='Horticulture/Forestry' ORDER BY Commodity";
ds = new System.Data.DataSet();
ds = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.ConnectionStrings["JoinFB_Conn"].ConnectionString,
System.Data.CommandType.Text, sSQL);
dtlList3.DataSource = ds;
dtlList3.DataBind();
sSQL = "SELECT CMDY_CODE, Commodity, Category FROM _WJ_TBL_Commodity WHERE Category='Trees/Vines' ORDER BY Commodity";
ds = new System.Data.DataSet();
ds = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.ConnectionStrings["JoinFB_Conn"].ConnectionString,
System.Data.CommandType.Text, sSQL);
dtlList4.DataSource = ds;
dtlList4.DataBind();
sSQL = "SELECT CMDY_CODE, Commodity, Category FROM _WJ_TBL_Commodity WHERE Category='Vegetables' ORDER BY Commodity";
ds = new System.Data.DataSet();
ds = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.ConnectionStrings["JoinFB_Conn"].ConnectionString,
System.Data.CommandType.Text, sSQL);
dtlList5.DataSource = ds;
dtlList5.DataBind();
}
protected void dlQuantityChanged1(object sender, EventArgs e)
{
DropDownList _dl = (DropDownList)sender;
DataListItem _dlItem = (DataListItem)_dl.NamingContainer;
// get id of row
CheckBox _chk = (CheckBox)_dlItem.FindControl("chk1");
CheckCMDY(_chk, _dl);
}
protected void dlQuantityChanged2(object sender, EventArgs e)
{
DropDownList _dl = (DropDownList)sender;
DataListItem _dlItem = (DataListItem)_dl.NamingContainer;
// get id of row
CheckBox _chk = (CheckBox)_dlItem.FindControl("chk1");
CheckCMDY(_chk, _dl);
}
protected void dlQuantityChanged3(object sender, EventArgs e)
{
DropDownList _dl = (DropDownList)sender;
DataListItem _dlItem = (DataListItem)_dl.NamingContainer;
// get id of row
CheckBox _chk = (CheckBox)_dlItem.FindControl("chk1");
CheckCMDY(_chk, _dl);
}
protected void dlQuantityChanged4(object sender, EventArgs e)
{
DropDownList _dl = (DropDownList)sender;
DataListItem _dlItem = (DataListItem)_dl.NamingContainer;
// get id of row
CheckBox _chk = (CheckBox)_dlItem.FindControl("chk1");
CheckCMDY(_chk, _dl);
}
protected void dlQuantityChanged5(object sender, EventArgs e)
{
DropDownList _dl = (DropDownList)sender;
DataListItem _dlItem = (DataListItem)_dl.NamingContainer;
// get id of row
CheckBox _chk = (CheckBox)_dlItem.FindControl("chk1");
CheckCMDY(_chk, _dl);
}
private void CheckCMDY(CheckBox chk, DropDownList dl)
{
if (dl.SelectedIndex > 0)
chk.Checked = true;
}
protected void Bind_Unit_1(object sender, DataListItemEventArgs e)
{
Bind_DDL_Unit(e, dtlList1, "ddl1");
}
protected void Bind_Unit_2(object sender, DataListItemEventArgs e)
{
Bind_DDL_Unit(e, dtlList2, "ddl1");
}
protected void Bind_Unit_3(object sender, DataListItemEventArgs e)
{
Bind_DDL_Unit(e, dtlList3, "ddl1");
}
protected void Bind_Unit_4(object sender, DataListItemEventArgs e)
{
Bind_DDL_Unit(e, dtlList4, "ddl1");
}
protected void Bind_Unit_5(object sender, DataListItemEventArgs e)
{
Bind_DDL_Unit(e, dtlList5, "ddl1");
}
protected void Bind_DDL_Unit(DataListItemEventArgs e, DataList dtlList, string cmdy_ddl)
{
string sSQL;
DropDownList ddl;
int x;
sSQL = "select DISTINCT m.Unit_CODE FROM _WJ_TBL_Unit m, _WJ_TBL_Commodity s WHERE s.CMDY_CODE=#CMDY_CODE and m.unit_Code=s.unit_Code";
// Retrieve Unit Code first
x = Convert.ToInt32(Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteScalar(ConfigurationManager.ConnectionStrings["JoinFB_Conn"].ConnectionString,
System.Data.CommandType.Text, sSQL, new SqlParameter("#CMDY_CODE", dtlList.DataKeys[dtlList.Items.Count])));
if (x > 0)
{
sSQL = "SELECT Unit, Unit_Value FROM _WJ_TBL_Unit WHERE Unit_Code=#Unit_Code";
DataSet ds = new DataSet();
ds = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(ConfigurationManager.ConnectionStrings["JoinFB_Conn"].ConnectionString,
System.Data.CommandType.Text, sSQL, new SqlParameter("#Unit_Code", x));
ddl = (DropDownList)(e.Item.FindControl(cmdy_ddl));
ddl.DataSource = ds;
ddl.DataTextField = "Unit";
ddl.DataValueField = "Unit_Value";
ddl.DataBind();
}
}
protected void btnNext_OnClick(object sender, EventArgs e)
{
TESTReg.Membership MemberInfo = new TESTReg.Membership();
MemberInfo = (TESTReg.Membership)Session.Contents["Membership"];
//MemberInfo.CMDY.Clear();
//MemberInfo.Produce.Clear();
MemberInfo.Commodities.Clear();
// Process Datalist
ProcessCMDY(dtlList1, MemberInfo, "ddl1", "chk1", "lblTitle1");
ProcessCMDY(dtlList2, MemberInfo, "ddl1", "chk1", "lblTitle1");
ProcessCMDY(dtlList3, MemberInfo, "ddl1", "chk1", "lblTitle1");
ProcessCMDY(dtlList4, MemberInfo, "ddl1", "chk1", "lblTitle1");
ProcessCMDY(dtlList5, MemberInfo, "ddl1", "chk1", "lblTitle1");
if (chkOCC.Checked)
MemberInfo.Organic_Crop_Cert = true;
else
MemberInfo.Organic_Crop_Cert = false;
if (chkUSDA.Checked)
MemberInfo.USDA_Cons_Prog = true;
else
MemberInfo.USDA_Cons_Prog = false;
Session.Contents["Membership"] = MemberInfo;
if (Request.QueryString["Mode"] == "Edit")
Response.Redirect("confirmation.aspx");
else
Response.Redirect("Payment.aspx");
}
private void ProcessCMDY(DataList dl, TESTReg.Membership MemberInfo, string ddlist, string chkbox, string lbel)
{
// Saves commodity checked
foreach (DataListItem di in dl.Items)
{
DropDownList ddl;
CheckBox chk;
Label lbl;
ddl = (DropDownList)(di.FindControl(ddlist));
chk = (CheckBox)(di.FindControl(chkbox));
lbl = (Label)(di.FindControl(lbel));
if (chk.Checked)
{
TESTReg.Commodity Commodities = new TESTReg.Commodity();
//MemberInfo.CMDY.Add(dl.DataKeys[di.ItemIndex]);
//MemberInfo.Produce.Add(ddl.SelectedItem.Value);
Commodities.Cmdy_Code = dl.DataKeys[di.ItemIndex].ToString();
Commodities.Unit_Value = Convert.ToString(ddl.SelectedItem.Value);
Commodities.Unit_Amt = ddl.SelectedItem.Text;
Commodities.Cmdy = lbl.Text;
Commodities.SelectedItem = chk.Checked;
MemberInfo.Commodities.Add(Commodities);
}
}
//string t1;
//string t2;
//for (int i = 0; i < MemberInfo.Commodities.Count; i++)
//{
// t1 = MemberInfo.Commodities[i].Cmdy;
// t2 = MemberInfo.Commodities[i].Unit_Amt;
//}
//return MemberInfo;
}
}

How to Bind Data in Grid View

<%# Page Title="" Language="C#" MasterPageFile="~/firstpage.master" AutoEventWireup="true"
CodeFile="MasterCodes.aspx.cs" Inherits="MasterCodes" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" LoadScriptsBeforeUI="false"
EnablePartialRendering="false">
</asp:ScriptManager>
<div class="contentarea">
<br />
<asp:Label ID="lblsub" Style="overflow-x: auto; margin-left: 20px;" runat="server" Text="Station master data entry" />
</div>
<div style="overflow: auto; padding: 10px 20px;">
<br />
<asp:Label ID="lblcontractnumber" runat="server" Text="Station Code" Font-Names="Calibri"></asp:Label>
<asp:TextBox ID="txtcontractnumber" runat="server"></asp:TextBox>
</div>
<div style="overflow: auto; padding: 10px 20px;">
<asp:Label ID="Label2" runat="server" Text="Station Name: " Font-Names="Calibri"></asp:Label>
<asp:Label ID="Label3" runat="server" Text="3A1-Elevated" Font-Names="Calibri"></asp:Label>
</div>
<div style="overflow: auto; padding: 10px 20px;">
<asp:GridView ID="grd_mastercodes" runat="server" HeaderStyle-BackColor="#f79646" HeaderStyle-ForeColor="White"
HeaderStyle-Font-Bold="false" Font-Names="Calibri"
Font-Size="Medium" RowStyle-BackColor="White" HeaderStyle-BorderColor="Black"
AutoGenerateColumns="False" RowStyle-BorderColor="Black" OnRowCreated="grd_mastercodes_RowCreated">
</asp:GridView>
</div>
</asp:Content>
code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Configuration;
using AjaxControlToolkit;
public partial class MasterCodes : System.Web.UI.Page
{
bool rowset = false;
GridView dgv = new GridView();
// For First gridview
String[] gridColumnNames_mastercodes = { "S.NO", "WBE Codes", "BOQ ID", "Description", "Unit", "Budgeted Quantity", "Variation Quantity", "Revised Quantity" };
String[] gridColumnwidth_mastercodes = { "50px", "200px", "200px", "350px", "100px", "100px", "100px", "100px" };
int[] gridColumnWidth_mastercodes = { 3, 15, 15, 45, 10, 10, 10, 10 };
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
// For First gridview
DataTable dt = new DataTable();
for (int c = 0; c < gridColumnNames_mastercodes.Length; c++)
{
DataColumn dcol = new DataColumn(c.ToString(), typeof(System.Int32));
dcol.ColumnName = gridColumnNames_mastercodes[c];
dt.Columns.Add(dcol);
TemplateField tf = new TemplateField();
tf.HeaderStyle.Font.Bold = false;
tf.HeaderText = gridColumnNames_mastercodes[c];
tf.HeaderStyle.Width = new Unit(gridColumnwidth_mastercodes[c]);
grd_mastercodes.Columns.Add(tf);
}
for (int i = 0; i < 15; i++)
{
dt.Rows.Add(dt.NewRow());
}
grd_mastercodes.DataSource = dt;
grd_mastercodes.DataBind();
}
}
catch (Exception ex)
{
string msg = ex.Message.ToString();
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('" + msg + "');", true);
}
}
//For First Gridview
protected void grd_mastercodes_RowCreated(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int rc = 0; rc < grd_mastercodes.Columns.Count; rc++)
{
TextBox txtBox = new TextBox();
txtBox.ID = "Excel_mastercodes" + e.Row.RowIndex + "_" + rc;
txtBox.MaxLength = gridColumnWidth_mastercodes[rc];
txtBox.Width = new Unit(gridColumnwidth_mastercodes[rc]);
txtBox.BorderStyle = BorderStyle.None;
e.Row.Cells[rc].Controls.Add(txtBox);
e.Row.Cells[rc].Attributes.Add("onKeyDown", "enter(this);");
if (rc == 3)
txtBox.Style["text-align"] = "left";
else
txtBox.Style["text-align"] = "center";
}
}
}
catch (Exception ex)
{
string msg = ex.Message.ToString();
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('" + msg + "');", true);
}
}
}
I need retrieve data from sql db . I know that still i din't connect to db but i am struggle where to write sql statement in above code to retrieve data. If any one know means please help me.
Tanks
I have an sample data for binding the gridview, You can customize accordingly
ASPX CODE
<asp:GridView ID="grdhospital"
runat="server"
AutoGenerateColumns="false"
Width="100%"
border="1"
AllowPaging="true"
OnPreRender="PreRenderGrid"
PageSize="2"
OnDataBound="grdhospital_DataBound"
OnPageIndexChanging="grdhospital_PageIndexChanging"
Style="border: 1px solid #E5E5E5;"
CellPadding="3"
CssClass="hoverTable"
DataKeyNames="Id"
OnRowCommand="grdhospital_RowCommand"
OnRowDeleting="grdhospital_RowDeleting"
EmptyDataText="No records found"
ShowFooter="false"
HeaderStyle-CssClass="k-grid td"
EnableSortingAndPagingCallbacks="false">
<AlternatingRowStyle CssClass="k-alt" />
<Columns>
<asp:TemplateField HeaderText="Select" ItemStyle-Width="5">
<ItemTemplate>
<asp:CheckBox ID="chkDelete" runat="server" onClick="Check_Click(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="hospital_name" HeaderText="Hospital Name" ItemStyle-Width="30" />
<asp:BoundField DataField="description" HeaderText="Description" ItemStyle-Width="30" />
<asp:BoundField DataField="active" HeaderText="Active" ItemStyle-Width="30" />
<asp:TemplateField HeaderText="Action" HeaderStyle-Width="5%">
<ItemTemplate>
<asp:ImageButton ID="btnEdit" AlternateText="Edit" ImageUrl="~/images/edit.png" runat="server" Width="15" Height="15" CommandName="eEdit" CommandArgument='<%# Eval("Id") %>' CausesValidation="false" />
<asp:ImageButton ID="btnDelete" AlternateText="Delete" ImageUrl="~/images/delete.png" runat="server" Width="15" Height="15" CommandName="Delete" CommandArgument='<%# Eval("Id") %>' CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this record?')" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
CS DataBindCode:-
First we need to write BindGrid() method like below
public void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand("Select * from yourtablename");
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
grdhospital.DataSource = dt;
grdhospital.DataBind();
}
}
}
}
Then we need to call the above method on Page_load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
Hope it helps you.

How to set <ItemTemplate> to a datasource?

How can I set the datasource of an ItemTemplate? Right now I have one that is wrapped by a datarepeater and that is how I databind to it.
<asp:Repeater ID="rptTotal" runat="server">
<ItemTemplate>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
<td>Four</td>
</tr>
</ItemTemplate>
</asp:Repeater>
You cant set the datasource for the item template, you can set it for the repeater in the code behind using
rptTotal.DataSource = datasourcename

System.MissingMemberException was unhandled by user code

I'm using this code:
Dim VehiclesTable1 = dsVehicleList.Tables(0)
Dim VT1 = (From d In VehiclesTable1.AsEnumerable _
Select VehicleTypeName = d.Item("VehicleTypeName") _
, VTypeID = d.Item("VTypeID") _
, ImageURL = d.Item("ImageURL") _
, DailyRate = d.Item("DailyRate") _
, RateID = d.Item("RateID")).Distinct
its linq to dataset
and I Take Data on THis Rotator:
<telerik:RadRotator ID="RadRotatorVehicleType" runat="server" Width="620px" Height="145"
ItemWidth="155" ItemHeight="145" ScrollDirection="Left" FrameDuration="1" RotatorType="Buttons">
<ItemTemplate>
<div style="text-align: center; cursor: pointer; width: 150px">
<asp:Image ID="ImageVehicleType" runat="server" Width="150" ImageUrl='<%# Container.DataItem("ImageURL") %>' />
<asp:Label ID="lblVehicleType" runat="server" Text='<%# Container.DataItem("VehicleTypeName") %>'
Font-Bold="true"></asp:Label>
<br />
<asp:Label ID="lblDailyRate" runat="server" Text='<%# Container.DataItem("DailyRate") %>'
Visible="False"></asp:Label>
<input id="HiddenVehicleTypeID" type="hidden" value='<%# Container.DataItem("VTypeID") %>'
name="HiddenVehicleTypeID" runat="server" />
<input id="HiddenRateID" type="hidden" value='<%# Container.DataItem("RateID") %>'
name="HiddenRateID" runat="server" />
</div>
</ItemTemplate>
<ControlButtons LeftButtonID="img_left" RightButtonID="img_right" />
</telerik:RadRotator>
and I got this Exception:
No default member found for type 'VB$AnonymousType_0(Of Object,Object,Object,Object,Object)'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.MissingMemberException: No default member found for type 'VB$AnonymousType_0(Of Object,Object,Object,Object,Object)'.
I don't know whats up ?
Any help please.
Thanks for who tried to solve this
but I got solution:
using '<%# DataBinder.Eval(Container.DataItem,"ImageURL") %>'
instead of '<%# Container.DataItem("RateID") %>'
Thanks,
Thanks for who tried to solve this but I got solution:
using '<%# DataBinder.Eval(Container.DataItem,"ImageURL") %>'
instead of '<%# Container.DataItem("RateID") %>'
Thanks,

Eval ID on radiobutton in Datalist

my code gota datalist with radio button and iv made it single selectable onitemdatabound....now im trying to evaluate a hiddenfield on basis of selected radio button
my code goes like this
aspx code
<asp:DataList ID="DataList1" runat="server" RepeatColumns = "4" CssClass="datalist1"
RepeatLayout = "Table" OnItemDataBound="SOMENAMEItemBound"
CellSpacing="20" onselectedindexchanged="DataList1_SelectedIndexChanged">
<ItemTemplate>
<br />
<table cellpadding = "5px" cellspacing = "0" class="dlTable">
<tr>
<td align="center">
<a href="<%#Eval("FilePath")%>" target="_blank"><asp:Image ID="Image1" runat="server" CssClass="imu" ImageUrl = '<%# Eval("FilePath")%>'
Width = "100px" Height = "100px" style ="cursor:pointer" />
</td>
</tr>
<tr >
<td align="center">
<asp:RadioButton ID="rdb" runat="server" OnCheckedChanged="rdb_click" AutoPostBack="True" />
<asp:HiddenField ID="HiddenField1" runat="server" Value = '<%#Eval("ID")%>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
code behind
protected void SOMENAMEItemBound(object sender, DataListItemEventArgs e)
{
RadioButton rdb;
rdb = (RadioButton)e.Item.FindControl("rdb");
if (rdb != null)
rdb.Attributes.Add("onclick", "CheckOnes(this);");
}
protected void rdb_click(object sender, EventArgs e)
{
for (int i = 0; i < DataList1.Items.Count; i++)
{
RadioButton rdb;
rdb = (RadioButton)DataList1.Items[i].FindControl("rdb");
if (rdb != null)
{
if (rdb.Checked)
{
HiddenField hf = (HiddenField)DataList1.Items[i].FindControl("HiddenField1");
Response.Write(hf.Value);
}
}
}
}
the javascript im using...
function CheckOnes(spanChk){
var oItem = spanChk.children;
var theBox= (spanChk.type=="radio") ?
spanChk : spanChk.children.item[0];
xState=theBox.unchecked;
elm=theBox.form.elements;
for(i=0;i<elm.length;i++)
if(elm[i].type=="radio" &&
elm[i].id!=theBox.id)
{
elm[i].checked=xState;
}
}
iam getting an error like this
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near 'pload Demonstration|'.
is there any other way to do this or can nyone plz help to get rid of this problem
The problem is probably your Response.Write() call. Take a look at this blog post. It outlines the reasons why this particular exception shows up, and how to prevent it. Also take a look at this StackOverflow thread.
Okey lets just remove response write...now i have following in my codebehind...rest is same
label5.text=hf.value.ToString();
now i am able to evaluate Label when i use update panel nd nested updatepanel like this
<asp:UpdatePanel ID="UpdatePanel9" runat="server" >
<ContentTemplate>
<asp:DataList ID="DataList1" runat="server" RepeatColumns = "4"
CssClass="datalist1" OnItemDataBound="SOMENAMEItemBound"
CellSpacing="10" onselectedindexchanged="DataList1_SelectedIndexChanged"
HorizontalAlign="Center" Width="500px">
<ItemTemplate>
<br />
<table cellpadding = "5px" cellspacing = "0" class="dlTable">
<tr>
<td align="center">
<a href="<%#Eval("FilePath")%>" target="_blank"><asp:Image ID="Image1" runat="server" CssClass="imu" ImageUrl = '<%# Eval("FilePath")%>'
Width = "100px" Height = "100px" style ="cursor:pointer" />
</td>
</tr>
<tr >
<td align="center">
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<Triggers>
<asp:PostBackTrigger ControlID="rdb" />
</Triggers>
<ContentTemplate>
<asp:RadioButton ID="rdb" runat="server" OnCheckedChanged="rdb_click" AutoPostBack="true" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:HiddenField ID="HiddenField1" runat="server" Value = '<%#Eval("ID")%>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>
is there a way to to get this done without full page postback....asyncpostback trigger doesnt seem to work