model binding webform TryUpdateModel not Working - c#

I have a simple formview and simple modelbinding in webform:
<asp:FormView ID="frm" ItemType="SabaDoor2.Models.Content" SelectMethod="frm_GetItem" UpdateMethod="frm_UpdateItem" DataKeyNames="Id" runat="server">
<ItemTemplate>
<fieldset>
<legend>
<asp:Label ID="lblName" Text='<%# Item.Name %>' runat="server"></asp:Label>
</legend>
<p>
<label>descripion :</label>
<asp:Label ID="lblDescription" Text='<%# Item.Description %>' runat="server"></asp:Label>
</p>
<p>
<asp:Button ID="btnEdit" Text="edit" CommandName="Edit" runat="server" />
</p>
</fieldset>
</ItemTemplate>
<EditItemTemplate>
<fieldset>
<legend>
<asp:Label ID="lblName" Text='<%# Item.Name %>' runat="server"></asp:Label>
</legend>
<p>
<label>description :</label>
<asp:TextBox ID="txt" Text='<%# Item.Description %>' runat="server" ></asp:TextBox>
</p>
<p>
<asp:Button CommandName="Update" ValidationGroup="mainPropertyGroup" runat="server" ID="btnUpdate" Text="update" />
<asp:Button CommandName="Cancel" ValidationGroup="mainPropertyGroup" runat="server" ID="btnCancel" Text="cancel" />
</p>
</fieldset>
<br />
</EditItemTemplate>
</asp:FormView>
and in code:
Models.Model1Container _db = new Models.Model1Container();
protected void Page_Load(object sender, EventArgs e)
{
bindEvents();
lblResult.ForeColor = System.Drawing.Color.Green;
lblResult.Text = "";
if (!IsPostBack)
{
}
else
{
}
}
private void bindEvents()
{
frm.ItemUpdated += frm_ItemUpdated;
}
void frm_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
{
if (e.Exception == null)
{
lblResult.ForeColor = System.Drawing.Color.Green;
lblResult.Text = "done!";
}
else
{
lblResult.ForeColor = System.Drawing.Color.Red;
lblResult.Text = "error:" + e.Exception.Message;
e.KeepInEditMode = true;
}
}
public SabaDoor2.Models.Content frm_GetItem([System.Web.ModelBinding.QueryString("Id")]int? Id)
{
return _db.Contents.Find(Id);
}
// The id parameter name should match the DataKeyNames value set on the control
public void frm_UpdateItem(int Id)
{
SabaDoor2.Models.Content item = null;
item = _db.Contents.Find(Id);
if (item == null)
{
// The item wasn't found
ModelState.AddModelError("", String.Format("Item with id {0} was not found", Id));
return;
}
var result = TryUpdateModel(item);
if (ModelState.IsValid)
{
_db.SaveChanges();
}
}
public override void Dispose()
{
_db.Dispose();
base.Dispose();
}
tryUpdateModel return true but my model(description field) doesn't update
:(

I find out logical error:
i have to initialize BindItem.Name property instd of item.Name
Text='<%# BindItem.Description %>'

Related

Using dropdownlists to pull data from Excel spreadsheet

Is there a way that I can use an excel spreadsheet that would then feed into my html / c# program. I have as many as 10 dropdownlists(DDL) that all have different values(example: {Ram [8, 16, 32, 64]} and another being {City[HSV, DAL, SLC, DNV]}, hard-coded into them in the html layer. Those DDL's names and values represent data that is in an excel spreadsheet. I need to write code that pulls certain data from the spreadsheet if and only if that particular value was selected within the DDL. ALL of the things I have tried looking through try to get me to import the spreadsheet and use it like a spreadsheet inside a panel or something. I need to have a way that lets me tinker with it more like a database. I need to be able to write functions/statements that operate like SQL Queries. Thank you for taking the time to read this and your attempt at helping me out.
HTML
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Find.aspx.cs" Inherits="VirginiaCollege.Find" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server" BorderColor="White" BorderStyle="None">
<br />
<br />
<asp:Label ID="Label11" runat="server" Text="Select what type of user you are..."></asp:Label>
<br />
<br />
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>IT Admin</asp:ListItem>
<asp:ListItem>Executive Management</asp:ListItem>
<asp:ListItem>Academic Director</asp:ListItem>
<asp:ListItem>Teachers</asp:ListItem>
<asp:ListItem>Students</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:Button ID="btSubmitUserType" runat="server" BorderStyle="Ridge" Text="Submit Selection" BackColor="#CCCCCC" OnClick="btSubmitUserType_Click" />
<br />
<br />
</asp:Panel>
<asp:Panel ID="Panel6" runat="server" Visible="false" >
<asp:Label ID="lblEnterPsWrd" runat="server" Text="Please enter the password"></asp:Label>
<br />
<br />
<asp:TextBox ID="tbPassword" runat="server" placeholder="Password" > </asp:TextBox>
<asp:Label ID="Label12" runat="server" Text="Password = Admin"></asp:Label>
<br />
<br />
<asp:Button ID="btnSubmitUserTypePsWrd" runat="server" BorderStyle="Ridge" Text="Submit Password" BackColor="#CCCCCC" OnClick="btSubmitUserTypePsWrd_Click" />
</asp:Panel>
<asp:Panel ID="Panel7" runat="server" Visible="false" >
<asp:Label ID="lblEnterPsWrd2" runat="server" Text="Please enter the password"></asp:Label>
<br />
<br />
<asp:TextBox ID="tbPassword2" runat="server" placeholder="Password" ></asp:TextBox>
<asp:Label ID="Label13" runat="server" Text="Password = Director"></asp:Label>
<br />
<br />
<asp:Button ID="btnSubmitUserTypePsWrd2" runat="server" BorderStyle="Ridge" Text="Submit Password" BackColor="#CCCCCC" OnClick="btSubmitUserTypePsWrd2_Click" />
</asp:Panel>
<asp:Panel ID="Panel8" runat="server" Visible="false" >
<asp:Label ID="lblEnterPsWrd3" runat="server" Text="Please enter the password"></asp:Label>
<br />
<br />
<asp:TextBox ID="tbPassword3" runat="server" placeholder="Password" ></asp:TextBox>
<asp:Label ID="Label14" runat="server" Text="Password = Student"></asp:Label>
<br />
<br />
<asp:Button ID="btnSubmitUserTypePsWrd3" runat="server" BorderStyle="Ridge" Text="Submit Password" BackColor="#CCCCCC" OnClick="btSubmitUserTypePsWrd3_Click" />
</asp:Panel>
<asp:Panel ID="Panel2" runat="server" Visible="false">
<asp:CheckBox ID="cbCity" runat="server" Text="City" />
<br />
<asp:CheckBox ID="cbComputerType" runat="server" Text="Computer Type" />
<br />
<asp:CheckBox ID="cbDepartment" runat="server" Text="Department" />
<br />
<asp:CheckBox ID="cbRoomNumber" runat="server" Text="Room Number" />
<br />
<asp:CheckBox ID="cbComputerPosition" runat="server" Text="Computer Postition" />
<br />
<asp:CheckBox ID="cbGraphicsCard" runat="server" Text="Graphics Card" />
<br />
<asp:CheckBox ID="cbRAM" runat="server" Text="RAM" />
<br />
<asp:CheckBox ID="cbProgram" runat="server" Text="Program" />
<br />
<asp:CheckBox ID="cbInstallDate" runat="server" Text="Install Date" />
<br />
<asp:CheckBox ID="cbExperationDate" runat="server" Text="Experation Date" />
<br />
<br />
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit Search Criteria" />
</asp:Panel>
<asp:Panel ID="Panel3" runat="server" Visible="false">
<asp:CheckBox ID="cbComputerType2" runat="server" Text="Computer Type" />
<br />
<asp:CheckBox ID="cbDepartment2" runat="server" Text="Department" />
<br />
<asp:CheckBox ID="cbRoomNumber2" runat="server" Text="Room Number" />
<br />
<asp:CheckBox ID="cbGraphicsCard2" runat="server" Text="Graphics Card" />
<br />
<asp:CheckBox ID="cbRAM2" runat="server" Text="RAM" />
<br />
<asp:CheckBox ID="cbProgram2" runat="server" Text="Program" />
<br />
<br />
<br />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Submit Search Criteria" />
</asp:Panel>
<asp:Panel ID="Panel4" runat="server" Visible="false">
<asp:CheckBox ID="cbComputerType3" runat="server" Text="Computer Type" />
<br />
<asp:CheckBox ID="cbGraphicsCard3" runat="server" Text="Department" />
<br />
<asp:CheckBox ID="cbRAM3" runat="server" Text="Room Number" />
<br />
<asp:CheckBox ID="cbProgram3" runat="server" Text="Graphics Card" />
<br />
<br />
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Submit Search Criteria" />
</asp:Panel>
<br />
<br />
<br />
<asp:Panel ID="Panel5" runat="server" Visible="false" HorizontalAlign="Left">
<asp:Label ID="Label1" runat="server" Text="Select City..." Visible="False"></asp:Label>
<asp:DropDownList ID="ddlCity" runat="server" Visible="False">
<asp:ListItem>HSV</asp:ListItem>
<asp:ListItem>DAL</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="Label2" runat="server" Text="Select Computer Type" Visible="False"></asp:Label>
<asp:DropDownList ID="ddlComputerType" runat="server" Visible="False">
<asp:ListItem>CSTU</asp:ListItem>
<asp:ListItem>CAD</asp:ListItem>
<asp:ListItem>Staff</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="Label3" runat="server" Text="Select Department" Visible="False"></asp:Label>
<asp:DropDownList ID="ddlDepartment" runat="server" Visible="False">
<asp:ListItem>3D Imaging</asp:ListItem>
<asp:ListItem>Accounting</asp:ListItem>
<asp:ListItem>Business</asp:ListItem>
<asp:ListItem>Recruiting</asp:ListItem>
<asp:ListItem>Management</asp:ListItem>
<asp:ListItem>Teaching</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="Label4" runat="server" Text="Select Room Number" Visible="False"></asp:Label>
<asp:DropDownList ID="ddlRoomNumber" runat="server" Visible="False">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
<asp:ListItem>13</asp:ListItem>
<asp:ListItem>14</asp:ListItem>
<asp:ListItem>15</asp:ListItem>
<asp:ListItem>16</asp:ListItem>
<asp:ListItem>17</asp:ListItem>
<asp:ListItem>18</asp:ListItem>
<asp:ListItem>19</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
<asp:ListItem>21</asp:ListItem>
<asp:ListItem>22</asp:ListItem>
<asp:ListItem>23</asp:ListItem>
<asp:ListItem>24</asp:ListItem>
<asp:ListItem>25</asp:ListItem>
<asp:ListItem>26</asp:ListItem>
<asp:ListItem>27</asp:ListItem>
<asp:ListItem>28</asp:ListItem>
<asp:ListItem>29</asp:ListItem>
<asp:ListItem>30</asp:ListItem>
<asp:ListItem>31</asp:ListItem>
<asp:ListItem>32</asp:ListItem>
<asp:ListItem>33</asp:ListItem>
<asp:ListItem>34</asp:ListItem>
<asp:ListItem>35</asp:ListItem>
<asp:ListItem>36</asp:ListItem>
<asp:ListItem>37</asp:ListItem>
<asp:ListItem>38</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="Label5" runat="server" Text="Select Computer Position" Visible="False"></asp:Label>
<asp:DropDownList ID="ddlComputerPosition" runat="server" Visible="False">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
<asp:ListItem>13</asp:ListItem>
<asp:ListItem>14</asp:ListItem>
<asp:ListItem>15</asp:ListItem>
<asp:ListItem>16</asp:ListItem>
<asp:ListItem>17</asp:ListItem>
<asp:ListItem>18</asp:ListItem>
<asp:ListItem>19</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
<asp:ListItem>21</asp:ListItem>
<asp:ListItem>22</asp:ListItem>
<asp:ListItem>23</asp:ListItem>
<asp:ListItem>24</asp:ListItem>
<asp:ListItem>25</asp:ListItem>
<asp:ListItem>26</asp:ListItem>
<asp:ListItem>27</asp:ListItem>
<asp:ListItem>28</asp:ListItem>
<asp:ListItem>29</asp:ListItem>
<asp:ListItem>30</asp:ListItem>
<asp:ListItem>31</asp:ListItem>
<asp:ListItem>32</asp:ListItem>
<asp:ListItem>33</asp:ListItem>
<asp:ListItem>34</asp:ListItem>
<asp:ListItem>35</asp:ListItem>
<asp:ListItem>36</asp:ListItem>
<asp:ListItem>37</asp:ListItem>
<asp:ListItem>38</asp:ListItem>
<asp:ListItem>39</asp:ListItem>
<asp:ListItem>40</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="Label6" runat="server" Text="Select Graphics Card" Visible="False"></asp:Label>
<asp:DropDownList ID="ddlGraphicsCard" runat="server" Visible="False">
<asp:ListItem>Dell Nvidia Tesla K40 GPU Computing Processor</asp:ListItem>
<asp:ListItem>Dell 6 GB Nvidia Tesla K20X GPU Computing Processor</asp:ListItem>
<asp:ListItem>Dell 2 GB Nvidia Quadro K620 Graphics Card</asp:ListItem>
<asp:ListItem>Nvidia Quadro K6000 SDI I/O</asp:ListItem>
<asp:ListItem>VisionTek CryoVenom R9 290</asp:ListItem>
<asp:ListItem>Nvidia GeForce GTX 780</asp:ListItem>
<asp:ListItem>AMD Radeon R9 295X2</asp:ListItem>
<asp:ListItem>AMD Radeon R9 290</asp:ListItem>
<asp:ListItem>MSI R9 280X Gaming 3G</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="Label7" runat="server" Text="Select RAM" Visible="False"></asp:Label>
<asp:DropDownList ID="ddlRAM" runat="server" Visible="False">
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>16</asp:ListItem>
<asp:ListItem>32</asp:ListItem>
<asp:ListItem>64</asp:ListItem>
<asp:ListItem>128</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="Label8" runat="server" Text="Select Program" Visible="False"></asp:Label>
<asp:DropDownList ID="ddlProgram" runat="server" Visible="False">
<asp:ListItem>Xara Photo & Graphic 2013</asp:ListItem>
<asp:ListItem>Real-time 3D Character</asp:ListItem>
<asp:ListItem>Autodesk Inventor</asp:ListItem>
<asp:ListItem>Autodesk AutoCAD 2015</asp:ListItem>
<asp:ListItem>Autodesk AutoCAD 2014</asp:ListItem>
<asp:ListItem>Autodesk AutoCAD 2013</asp:ListItem>
<asp:ListItem>Autodesk AutoCAD 2012</asp:ListItem>
<asp:ListItem>Autodesk AutoCAD 2011</asp:ListItem>
<asp:ListItem>Autodesk AutoCAD 2010</asp:ListItem>
<asp:ListItem>QuickBooks 2017</asp:ListItem>
<asp:ListItem>QuickBooks !=2017</asp:ListItem>
<asp:ListItem>Visual Studio 2015</asp:ListItem>
<asp:ListItem>Visual Studio 2016</asp:ListItem>
<asp:ListItem>Visual Studio 2017</asp:ListItem>
<asp:ListItem>Visual Studio 2013</asp:ListItem>
<asp:ListItem>Visual Studio 2012</asp:ListItem>
<asp:ListItem>Visual Studio 2011</asp:ListItem>
<asp:ListItem>Visual Studio 2010</asp:ListItem>
<asp:ListItem>Visual Studio 2009</asp:ListItem>
<asp:ListItem>Visual Studio 2014</asp:ListItem>
<asp:ListItem>Office suite</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="Label9" runat="server" Text="Select Software Install Date" Visible="False"></asp:Label>
<asp:DropDownList ID="ddlSoftInstalDate" runat="server" Visible="False">
<asp:ListItem>JUN 2009</asp:ListItem>
<asp:ListItem>MAY 2010</asp:ListItem>
<asp:ListItem>JUN 2011</asp:ListItem>
<asp:ListItem>DEC 2012</asp:ListItem>
<asp:ListItem>DEC 2013</asp:ListItem>
<asp:ListItem>JAN 2014</asp:ListItem>
<asp:ListItem>JAN 2015</asp:ListItem>
<asp:ListItem>FEB 2015</asp:ListItem>
<asp:ListItem>JUL 2016</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="Label10" runat="server" Text="Select Software Expire Date" Visible="False"></asp:Label>
<asp:DropDownList ID="ddlSoftExpDate" runat="server" Visible="False">
<asp:ListItem>DEC 2013</asp:ListItem>
<asp:ListItem>DEC 2014</asp:ListItem>
<asp:ListItem>MAY 2015</asp:ListItem>
<asp:ListItem>DEC 2015</asp:ListItem>
<asp:ListItem>JAN 2016</asp:ListItem>
<asp:ListItem>JAN 2017</asp:ListItem>
<asp:ListItem>NOV 2018</asp:ListItem>
<asp:ListItem>DEC 2018</asp:ListItem>
<asp:ListItem>DEC 2019</asp:ListItem>
</asp:DropDownList>
</asp:Panel>
<asp:Panel ID="Panel40" runat="server" Visible="False"></asp:Panel>
<asp:CheckBoxList ID="CheckBoxList20" runat="server"></asp:CheckBoxList>
<asp:Panel ID="Panel50" runat="server" Visible="False"></asp:Panel>
</div>
</form>
</body>
</html>
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace VirginiaCollege
{
public partial class Find : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btSubmitUserType_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Text == "IT Admin" )
{
Panel6.Visible = true;
Panel1.Visible = false;
}
else if (DropDownList1.SelectedItem.Text == "Executive Management" )
{
Panel6.Visible = true;
Panel1.Visible = false;
}
else if (DropDownList1.SelectedItem.Text == "Academic Director")
{
Panel7.Visible = true;
Panel1.Visible = false;
}
else if (DropDownList1.SelectedItem.Text == "Teachers")
{
Panel8.Visible = true;
Panel1.Visible = false;
}
else if (DropDownList1.SelectedItem.Text == "Students")
{
Panel8.Visible = true;
Panel1.Visible = false;
}
}
protected void btSubmitUserTypePsWrd_Click(object sender, EventArgs e)
{
if (tbPassword.Text == "Admin")
{
Panel2.Visible = true;
Panel1.Visible = false;
Panel6.Visible = false;
}
}
protected void btSubmitUserTypePsWrd2_Click(object sender, EventArgs e)
{
if (tbPassword2.Text == "Director")
{
Panel3.Visible = true;
Panel1.Visible = false;
Panel7.Visible = false;
}
}
protected void btSubmitUserTypePsWrd3_Click(object sender, EventArgs e)
{
if (tbPassword3.Text == "Student")
{
Panel4.Visible = true;
Panel1.Visible = false;
Panel8.Visible = false;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (cbCity.Checked == true)
{
Panel5.Visible = true;
Panel2.Visible = false;
Label1.Visible = true;
DropDownList2.Visible = true;
}
if (cbComputerType.Checked == true)
{
Panel5.Visible = true;
Panel2.Visible = false;
Label2.Visible = true;
DropDownList3.Visible = true;
}
if (cbDepartment.Checked == true)
{
Panel5.Visible = true;
Panel2.Visible = false;
Label2.Visible = true;
DropDownList3.Visible = true;
}
if (cbRoomNumber.Checked == true)
{
Panel5.Visible = true;
Panel2.Visible = false;
Label3.Visible = true;
DropDownList4.Visible = true;
}
if (cbComputerPosition.Checked == true)
{
Panel5.Visible = true;
Panel2.Visible = false;
Label4.Visible = true;
DropDownList5.Visible = true;
}
if (cbGraphicsCard.Checked == true)
{
Panel5.Visible = true;
Panel2.Visible = false;
Label5.Visible = true;
DropDownList6.Visible = true;
}
if (cbRAM.Checked == true)
{
Panel5.Visible = true;
Panel2.Visible = false;
Label6.Visible = true;
DropDownList7.Visible = true;
}
if (cbProgram.Checked == true)
{
Panel5.Visible = true;
Panel2.Visible = false;
Label7.Visible = true;
DropDownList8.Visible = true;
}
if (cbInstallDate.Checked == true)
{
Panel5.Visible = true;
Panel2.Visible = false;
Label8.Visible = true;
DropDownList9.Visible = true;
}
if (cbExperationDate.Checked == true)
{
Panel5.Visible = true;
Panel2.Visible = false;
Label9.Visible = true;
DropDownList10.Visible = true;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
if (cbComputerType2.Checked == true)
{
Panel5.Visible = true;
Panel2.Visible = false;
Label1.Visible = true;
DropDownList2.Visible = true;
}
if (cbDepartment2.Checked == true)
{
Panel5.Visible = true;
Panel2.Visible = false;
Label2.Visible = true;
DropDownList3.Visible = true;
}
if (cbRoomNumber2.Checked == true)
{
Panel5.Visible = true;
Panel2.Visible = false;
Label3.Visible = true;
DropDownList4.Visible = true;
}
if (cbGraphicsCard2.Checked == true)
{
Panel5.Visible = true;
Panel2.Visible = false;
Label5.Visible = true;
DropDownList6.Visible = true;
}
if (cbRAM2.Checked == true)
{
Panel5.Visible = true;
Panel2.Visible = false;
Label6.Visible = true;
DropDownList7.Visible = true;
}
if (cbProgram2.Checked == true)
{
Panel5.Visible = true;
Panel2.Visible = false;
Label7.Visible = true;
DropDownList8.Visible = true;
}
}
protected void Button3_Click(object sender, EventArgs e)
{
if (cbComputerType3.Checked == true)
{
Panel5.Visible = true;
Panel2.Visible = false;
Label1.Visible = true;
DropDownList2.Visible = true;
}
if (cbGraphicsCard3.Checked == true)
{
Panel5.Visible = true;
Panel2.Visible = false;
Label5.Visible = true;
DropDownList6.Visible = true;
}
if (cbRAM3.Checked == true)
{
Panel5.Visible = true;
Panel2.Visible = false;
Label6.Visible = true;
DropDownList7.Visible = true;
}
if (cbProgram3.Checked == true)
{
Panel5.Visible = true;
Panel2.Visible = false;
Label7.Visible = true;
DropDownList8.Visible = true;
}
}
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}

Error handling for radiobutoon with textbox in asp.net webform

I need to display error if user selects "Other" Radio button and leaves it blank or enters wrong data in textbox. once user enters right data error goes away.
<asp:RadioButtonList ID="radyears" AutoPostBack="true" runat="server" Height="63px" Width="100px" OnSelectedIndexChanged="radyears_SelectedIndexChanged" Font-Names="Arial" Font-Size="12pt">
<asp:ListItem Selected="True" Value="15">15 Years</asp:ListItem>
<asp:ListItem Value="30">30 Years</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:RadioButtonList>
<asp:TextBox ID="txtother" runat="server" AutoPostBack="True" Font-Names="Arial" Font-Size="12pt" Width="150px" MaxLength="10"></asp:TextBox>
<asp:Label ID="lblothererror" runat="server" Font-Names="Arial" Font-Size="11pt" ForeColor="Red"></asp:Label>
Below is image showing how radiobutton shows with textbox and error message label next to textbox.
Here is image of radiobutton display
##### Here is my Try
if (radyears.SelectedValue == "Other")
{
if (String.IsNullOrEmpty(txtother.Text) || (!double.TryParse(txtother.Text, out years)))
{
lblothererror.Text = "Not Valid Input";
return;
}
else
{
lblothererror.Text = "valid number";
return;
}
Try this code :-
<asp:RadioButtonList ID="radyears" AutoPostBack="true" runat="server" Height="63px" Width="100px" Font-Names="Arial" Font-Size="12pt" OnSelectedIndexChanged="radyears_SelectedIndexChanged">
<asp:ListItem Selected="True" Value="15">15 Years</asp:ListItem>
<asp:ListItem Value="30">30 Years</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:RadioButtonList>
<asp:TextBox ID="txtother" runat="server" AutoPostBack="True" Font-Names="Arial" Font-Size="12pt" Width="150px" MaxLength="10" OnTextChanged="txtother_TextChanged"></asp:TextBox>
<asp:Label ID="lblothererror" runat="server" Font-Names="Arial" Font-Size="11pt" ForeColor="Red"></asp:Label>
and in aspx.cs page
protected void radyears_SelectedIndexChanged(object sender, EventArgs e)
{
if (radyears.SelectedValue == "Other")
{
lblothererror.Text = "Enter age in Textbox";
}
else
{
lblothererror.Text = "";
}
}
protected void txtother_TextChanged(object sender, EventArgs e)
{
if (radyears.SelectedValue == "Other")
{
double years = 100;
if (String.IsNullOrEmpty(txtother.Text) || (Convert.ToInt32(txtother.Text) > years))
{
lblothererror.Text = "Invalid";
}
else
{
lblothererror.Text = "valid";
}
}
else
{
lblothererror.Text = "";
}
}
Try this
Here is c# code
protected void radyears_SelectedIndexChanged(object sender, EventArgs e)
{
if (radyears.SelectedValue == "Other")
{
lblothererror.Text = "Enter value in Textbox please";
}
}
protected void txtother_TextChanged(object sender, EventArgs e)
{
if (radyears.SelectedValue == "Other")
{
double years = 123;//Any Value
if (String.IsNullOrEmpty(txtother.Text) || (!double.TryParse(txtother.Text, out years)))
{
lblothererror.Text = "Not Valid Input";
return;
}
else
{
lblothererror.Text = "valid number";
return;
}
}
else
{
lblothererror.Text = "";
return;
}
}
and here it is
<asp:RadioButtonList ID="radyears" AutoPostBack="true" runat="server" Height="63px" Width="100px" OnSelectedIndexChanged="radyears_SelectedIndexChanged" Font-Names="Arial" Font-Size="12pt">
<asp:ListItem Selected="True" Value="15">15 Years</asp:ListItem>
<asp:ListItem Value="30">30 Years</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:RadioButtonList>
<asp:TextBox ID="txtother" runat="server" OnTextChanged="txtother_TextChanged" Font-Names="Arial" Font-Size="12pt" Width="150px" MaxLength="10"></asp:TextBox>
<asp:Label ID="lblothererror" runat="server" Font-Names="Arial" Font-Size="11pt" ForeColor="Red"></asp:Label>

Add new record to Telerik RadGrid

I have the following Telerik RadGrid, which I'm using in C#:
<MasterTableView Width="100%" EditMode= "InPlace" ClientDataKeyNames="menuID" CommandItemDisplay= "Top">
<Columns>
<telerik:GridBoundColumn DataField="Name" HeaderText="Name" SortExpression="Name" UniqueName="Name"></telerik:GridBoundColumn>
<telerik:GridTemplateColumn UniqueName="Type" HeaderText="Type">
<ItemTemplate>
<asp:TextBox ID="Type" Text='<%# DataBinder.Eval(Container.DataItem, "Type") %>' runat="server"></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="List" HeaderText="List">
<ItemTemplate>
<asp:CheckBox ID="List" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "List") %>' />
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="loadAtStart" HeaderText="loadAtStart">
<ItemTemplate>
<asp:CheckBox ID="loadAtStart" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "loadAtStart") %>' />
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
The grid is populated with data, and working normally when editing the data.
But when I click on the Add New Record button provided by Telerik, then an empty row is added without any TextBox or CheckBox in the columns to edit in the new Row added. It's just an empty row. I presume that I have to create the controls dynamically in the called ItemDataBound event, but I didn't manage to find the actual code for this.
How do I solve this?
Please try with the below code snippet.
ASPX
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
AllowFilteringByColumn="true" AllowPaging="true" OnItemCommand="RadGrid1_ItemCommand">
<PagerStyle AlwaysVisible="true" />
<MasterTableView DataKeyNames="UniqueID" CommandItemDisplay="Top">
<Columns>
<telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:Button ID="Button1" runat="server" Text="Save All" OnClick="Button1_Click" />
ASPX.CS
public partial class aaaa : System.Web.UI.Page
{
public List<Employee> lstEmployee
{
get
{
if (Session["lstEmployee"] != null)
{
return (List<Employee>)Session["lstEmployee"];
}
else
{
return new List<Employee>();
}
}
set
{
Session["lstEmployee"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<Employee> list = new List<Employee>();
Employee obj = new Employee();
obj.ID = 1;
obj.Name = "Name1";
obj.UniqueID = Guid.NewGuid();
list.Add(obj);
obj = new Employee();
obj.ID = 2;
obj.Name = "Name2";
obj.UniqueID = Guid.NewGuid();
list.Add(obj);
lstEmployee = list;
}
}
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = lstEmployee;
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.InitInsertCommandName)
{
saveAllData();
lstEmployee.Insert(0, new Employee() { UniqueID = Guid.NewGuid() });
e.Canceled = true;
RadGrid1.Rebind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
saveAllData();
}
protected void saveAllData()
{
//Update Session
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
Guid UniqueID = new Guid(item.GetDataKeyValue("UniqueID").ToString());
Employee emp = lstEmployee.Where(i => i.UniqueID == UniqueID).First();
emp.Name = (item.FindControl("TextBox1") as TextBox).Text;
}
}
}
public class Employee
{
public Guid UniqueID { get; set; }
public int ID { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
public int weeknumber { get; set; }
}
I have modified the parameters according to my requirement and also included all CURD operations.
ASPX Page
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false"
OnNeedDataSource="RadGrid1_NeedDataSource"
AllowFilteringByColumn="true" AllowPaging="true" OnItemCommand="RadGrid1_ItemCommand">
<PagerStyle AlwaysVisible="true" />
<MasterTableView DataKeyNames="UniqueID" CommandItemDisplay="Top" AutoGenerateColumns="false" >
<Columns>
<telerik:GridEditCommandColumn ButtonType="FontIconButton" />
<telerik:GridButtonColumn CommandName="Delete" Text="Delete" ButtonType="FontIconButton" UniqueName="DeleteColumn" />
<telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn DataField="Sku" UniqueName="Sku" HeaderText="Sku">
<ItemTemplate>
<asp:Label ID="TxtSku" runat="server" Text='<%# Eval("Sku") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TxtSku" runat="server" Text='<%# Eval("Sku") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TxtSku" runat="server" Text='<%# Eval("Sku") %>'></asp:TextBox>
</InsertItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:Button ID="Button1" runat="server" Text="Save All" OnClick="Button1_Click" />
<telerik:RadAjaxManager runat="server">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="RadGrid1">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="RadGrid1" />
<telerik:AjaxUpdatedControl ControlID="Button1" />
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
</telerik:RadAjaxLoadingPanel>
CS Code
public List<ItemDetail> ItemDetail
{
get
{
if (Session["ItemDetail"] != null)
{
return (List<ItemDetail>)Session["ItemDetail"];
}
else
{
return new List<ItemDetail>();
}
}
set
{
Session["ItemDetail"] = value;
}
}
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = ItemDetail;
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.InitInsertCommandName)
{
}
if (e.CommandName == RadGrid.PerformInsertCommandName)
{
var item = (GridEditFormItem)e.Item;
var txtReferenceNumber = (TextBox)item.FindControl(id: "TxtSku");
var val = txtReferenceNumber.Text;
ItemDetail.Insert(0, new ItemDetail() { UniqueID = Guid.NewGuid(), Sku = val, ID = Convert.ToInt32(22) });
item.Edit = false;
RadGrid1.Rebind();
}
if (e.CommandName == RadGrid.UpdateCommandName)
{
var item = (GridEditFormItem)e.Item;
var txtReferenceNumber = (TextBox)item.FindControl(id: "TxtSku");
string requestId = item.GetDataKeyValue(keyName: "UniqueID").ToString().Trim();
Guid RequestIndentifier = Guid.Parse(requestId);
var val = txtReferenceNumber.Text;
var itemFind = ItemDetail.Where(x => x.UniqueID == RequestIndentifier);
itemFind.FirstOrDefault().Sku = val;
item.Edit = false;
RadGrid1.Rebind();
}
if (e.CommandName == RadGrid.DeleteCommandName)
{
var item = (GridDataItem)e.Item;
string requestId = item.GetDataKeyValue(keyName: "UniqueID").ToString().Trim();
Guid RequestIndentifier = Guid.Parse(requestId);
ItemDetail.RemoveAll(x => x.UniqueID == RequestIndentifier);
RadGrid1.Rebind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SaveAllData();
}
private void SaveAllData()
{
//Update Session
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
Guid UniqueID = new Guid(item.GetDataKeyValue("UniqueID").ToString());
ItemDetail emp = ItemDetail.Where(i => i.UniqueID == UniqueID).First();
emp.Sku = (item.FindControl("TxtSku") as TextBox).Text;
}
}
}
public class ItemDetail
{
public Guid UniqueID { get; set; }
public int ID { get; set; }
public string Sku { get; set; }
}
if (!Page.IsPostBack)
{
List<ItemDetail> list = new List<ItemDetail>();
ItemDetail obj = new ItemDetail
{
ID = 1,
Sku = "Name1",
UniqueID = Guid.NewGuid()
};
list.Add(obj);
obj = new ItemDetail();
obj.ID = 2;
obj.Sku = "Name2";
obj.UniqueID = Guid.NewGuid();
list.Add(obj);
ItemDetail = list;
}
Hope this would help you :-)
I'm not really a fan of any of these solutions because they require the use of Session. Session has it's place but I believe it's used more as a crutch.
You're already sending the RadGrid information back to the server-side via ViewState due to the PostBack so why not use it's information instead of relying on an in-memory store.
Example of Adding and Removing an item from a Grid. Yes, it does require iterating items to rebuild a List but it's an alternative to Session so I believe it has merit.
protected void addValueToRecentTagList(string text)
{
List<string> items = new List<string>();
foreach(GridDataItem row in RadGridNewTags.Items)
items.Add(row["tag"].Text);
items.Add(text);
RadGridNewTags.DataSource = items;
RadGridNewTags.DataBind();
}
protected void removeValueFromRecentTagList(string text)
{
List<string> items = new List<string>();
bool rebind = false;
foreach (GridDataItem row in RadGridNewTags.Items)
{
if (row["tag"].Text == text)
rebind = true;
else
items.Add(row["tag"].Text);
}
if (rebind)
{
RadGridNewTags.DataSource = items;
RadGridNewTags.DataBind();
}
}
You have to insert a control in the EditItemTemplate to display in Edit or Insert mode.
Or you can add an InsertItem Template and place a control in there:
<telerik:GridTemplateColumn UniqueName="loadAtStart" HeaderText="loadAtStart">
<ItemTemplate>
<asp:CheckBox ID="loadAtStart" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "loadAtStart") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="cbxEditLoadAtStart" runat="server" Checked='<%# Bind("loadAtStart") %>' />
</EditItemTemplate>
<InsertItemTemplate>
<asp:CheckBox ID="cbxInsertLoadAtStart" runat="server" />
</InsertItemTemplate>
</telerik:GridTemplateColumn>

trigger for a button in a gridview in update panel

I have a gridview in an update panel and it is working fine. However on the click of the edit button I want to open the edit form. But the edit button click is not working. When i debug it, the method is working fine however the page doesn't postback. I have tried using triggers but to no avail.
<asp:UpdatePanel ID="upnlgrid" runat="server" UpdateMode="Conditional" >
<ContentTemplate>
<asp:GridView ID="gvGroupMaster" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanging="gvGroupMaster_SelectedIndexChanging" OnSelectedIndexChanged="gvGroupMaster_SelectedIndexChanged" OnRowDataBound="gvGroupMaster_RowDataBound" AllowSorting="true" OnPageIndexChanging="gvGroupMaster_PageIndexChanging" OnSorting="gvGroupMaster_Sorting"
ShowHeader="True" CssClass="tabledata" Width="100%" DataKeyNames="igroup_id">
<AlternatingRowStyle CssClass="pointer" />
<RowStyle CssClass="even pointer" />
<HeaderStyle CssClass="headings" />
<Columns>
<asp:TemplateField HeaderText="Sort Order" ItemStyle-CssClass="colmn1" SortExpression="isort_position" >
<ItemTemplate>
<%-- <asp:ImageButton ID="btnSort" OnClick="btnSortClick" imageurl="../images/order-sort-btnup.png" runat="server"></asp:ImageButton>--%>
<asp:Button ID="btnSort" class="sortord pointer" runat="server" OnClick="btnSortClick" ></asp:Button>
<asp:Button ID="btnSortDown" class="sortorddown pointer" runat="server" OnClick="btnSortDownClick"></asp:Button>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Group Name" SortExpression="strgroup_name" ItemStyle-CssClass="colmn2" >
<ItemTemplate>
<asp:Label ID="lblGroupName" runat="server"
Text='<%# Bind("strgroup_name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Short Name" SortExpression="strgroup_sname" ItemStyle-CssClass="colmn3" >
<ItemTemplate>
<asp:Label ID="lblShortName" runat="server"
Text='<%# Bind("strgroup_sname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Under" SortExpression="iparent_id" ItemStyle-CssClass="colmn4">
<ItemTemplate>
<asp:Label ID="lblUnder" runat="server"
Text='<%# Bind("strunder") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Nature Of Group" SortExpression="strnature_of_group" ItemStyle-CssClass="colmn5">
<ItemTemplate>
<asp:Label ID="lblNatureOfGroup" runat="server"
Text='<%# Bind("strnature_of_group") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Affect Gross Profit" SortExpression="straffect_gross_profits" ItemStyle-CssClass="colmn6">
<ItemTemplate>
<asp:Label ID="lblAffectGrossProfits" runat="server" Text='<%# Bind("straffect_gross_profits") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Nett DrCr Report" SortExpression="strnett_drcr_report" ItemStyle-CssClass="colmn7">
<ItemTemplate>
<asp:Label ID="lblNettDrcrReport" runat="server"
Text='<%# Bind("strnett_drcr_report") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Created By" SortExpression="icreated_by" ItemStyle-CssClass="colmn8">
<ItemTemplate>
<asp:Label ID="lblCreatedBy" runat="server"
Text='<%# Bind("strcreated_by") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Created On" SortExpression="dtcreated_on" ItemStyle-CssClass="colmn9">
<ItemTemplate>
<asp:Label ID="lblCreatedOn" runat="server"
Text='<%# Bind("dtcreated_on") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Updated On" SortExpression="dtupdated_on" ItemStyle-CssClass="colmn10">
<ItemTemplate>
<asp:Label ID="lbLastUpdatedOn" runat="server"
Text='<%# Bind("dtupdated_on") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Actions" ItemStyle-CssClass="colmn11">
<ItemTemplate>
<asp:Button id="btnInfo" runat="server" class="infoicon" Text='<%# Eval("igroup_id") %>' OnClick="infoclick"/>
<asp:Button id="btnEdit" runat="server" class="editicon" CausesValidation="false" Text='<%# Eval("igroup_id") %>' OnClick="editclick"/>
<asp:Button id="btnDelete" runat="server" class="deleteicon" Text='<%# Eval("igroup_id") %>' OnClick="deleteclick"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
codebehind for edit click
protected void editclick(object sender, EventArgs e)
{
try
{
//int rindex = (((GridViewRow)(((Button)(sender)).Parent.BindingContainer))).RowIndex;
Button EditButton = (Button)gvGroupMaster.Rows[(((GridViewRow)(((Button)(sender)).Parent.BindingContainer))).RowIndex].FindControl("btnEdit");
edit(EditButton.Text.ToString());
this.Session["edit"] = EditButton.Text.ToString();
}
catch
{
}
}
protected void edit(string editid)
{
try
{
GroupMasterClass gm = new GroupMasterClass();
CompanyMasterClass co = new CompanyMasterClass();
gm.igroup_id = Convert.ToInt32(editid);
ResultClass objres = gm.fn_GetGroupByIdForEdit();
if (objres.bStatus)
{
eslist<GroupMasterClass> OBJLIST = objres.objData as eslist<GroupMasterClass>;
if (OBJLIST.Count > 0)
{
co.strcompany_code = Request.Cookies["userinfo"]["companycode"].ToString();
ResultClass objress = co.fn_GetNameNumberStyle();
if (objress.bStatus)
{
eslist<CompanyMasterClass> OBJLISTS = objress.objData as eslist<CompanyMasterClass>;
if (OBJLISTS.Count > 0)
{
addfrm.Visible = true;
gridmain.Visible = false;
if (OBJLISTS[0].strname_style.ToString() == "PC")
{
txtGroupName.Text = misc.ToTitleCase(OBJLIST[0].strgroup_name);
txtGroupSname.Text = misc.ToTitleCase(OBJLIST[0].strgroup_sname);
}
if (OBJLISTS[0].strname_style.ToString() == "UC")
{
txtGroupName.Text = (OBJLIST[0].strgroup_name).ToUpper();
txtGroupSname.Text = (OBJLIST[0].strgroup_sname).ToUpper();
txtGroupName.Style.Add("text-transform", "uppercase");
txtGroupSname.Style.Add("text-transform", "uppercase");
}
if (OBJLISTS[0].strname_style.ToString() == "UG")
{
txtGroupName.Text = (OBJLIST[0].strgroup_name).ToUpper();
txtGroupSname.Text = (OBJLIST[0].strgroup_sname).ToUpper();
}
}
}
txtUnder.Text = OBJLIST[0].strunder;
txtNotes.Text = OBJLIST[0].strnotes;
for (int i = 0; i < OBJLIST.Count; i++)
{
CompanyMasterClass cm = new CompanyMasterClass();
string p = OBJLIST[0].strcompany_code.ToString();
string t = string.Empty;
string code = string.Empty;
int count = 0;
string[] availcompanycode = p.Split(',');
foreach (string k in availcompanycode)
{
t = k.ToString();
code += "'" + t.ToString() + "'" + ",";
count++;
}
cm.strcompany_code = code.TrimEnd(',');
ResultClass objrest = cm.fn_GetCompanyListByCompanycode();
if (objres.bStatus)
{
eslist<CompanyMasterClass> OBJLISTS = objrest.objData as eslist<CompanyMasterClass>;
if (OBJLISTS.Count > 0)
{
// listboxsource.Items.Clear();
listboxdestination.DataTextField = "strcompany_name";
listboxdestination.DataValueField = "strcompany_code";
listboxdestination.DataSource = OBJLISTS;
listboxdestination.DataBind();
}
}
ListItem itemnature = new ListItem();
if (OBJLIST[i].strnature_of_group == "A")
itemnature.Text = "Assets";
else if (OBJLIST[i].strnature_of_group == "E")
itemnature.Text = "Expenses";
else if (OBJLIST[i].strnature_of_group == "I")
itemnature.Text = "Income";
else if (OBJLIST[i].strnature_of_group == "L")
itemnature.Text = "Liabilities";
// itemnature.Value = OBJLIST[i].igroup_id.ToString();
ddlNature.Items.Add(itemnature);
ListItem itemaffects = new ListItem();
if (OBJLIST[i].straffect_gross_profits == "N")
itemaffects.Text = "No";
else if (OBJLIST[i].straffect_gross_profits == "Y")
itemaffects.Text = "Yes";
//itemaffects.Value = OBJLIST[i].igroup_id.ToString();
ddlGrossProfit.Items.Add(itemaffects);
ListItem itemnett = new ListItem();
if (OBJLIST[i].strnett_drcr_report == "N")
itemnett.Text = "No";
else if (OBJLIST[i].strnett_drcr_report == "Y")
itemnett.Text = "Yes";
// itemnett.Value = OBJLIST[i].igroup_id.ToString();
ddlNett.Items.Add(itemnett);
ListItem itemlock = new ListItem();
if (OBJLIST[i].strlock_status == "N")
itemlock.Text = "No";
else if (OBJLIST[i].strlock_status == "Y")
itemlock.Text = "Yes";
// itemnett.Value = OBJLIST[i].igroup_id.ToString();
ddlNett.Items.Add(itemlock);
}
}
}
}
catch
{
}
}
Any ideas?
Thanks,
Try using Button editbtn = (Button)sender; Rather than Button EditButton = (Button)gvGroupMaster.Rows[(((GridViewRow)(((Button)(sender)).Parent.BindingContainer))).RowIndex].FindControl("btnEdit");

How to transfer item from one datalist to other datalist?

I have a datalist
<asp:DataList ID="dlstImage" runat="server" RepeatDirection="Horizontal" RepeatColumns="5"
CellSpacing="8">
<ItemTemplate>
<asp:ImageButton ID="Image" runat="server" ImageUrl='<%#"~/Controls/ShowImage.ashx?FileName=" +DataBinder.Eval(Container.DataItem, "FilePath") %>'
OnCommand="Select_Command" CommandArgument='<%# Eval("Id").ToString() +";"+Eval("FilePath")+";"+Eval("Index") %>' /><br />
<asp:Label ID="lbl" runat="server" Text="Figure"></asp:Label><%# dlstImage.Items.Count + 1%>
</ItemTemplate>
</asp:DataList>
In which i am binding the image after uploading through uplodify upload, now i have one more datalist
and two btn up and down,
<asp:ImageButton ID="ibtnMoveUp" runat="server" ImageUrl="~/App_Themes/Default/Images/moveup.bmp"
Style="height: 16px" ToolTip="MoveUp The Item" />
<asp:ImageButton ID="ibtnMoveDown" runat="server" ImageUrl="~/App_Themes/Default/Images/movedown.bmp"
ToolTip="MoveDown The Item" />
<asp:DataList ID="dlstSelectedImages" runat="server" RepeatDirection="Horizontal"
RepeatColumns="5" CellSpacing="8">
<ItemTemplate>
<asp:ImageButton ID="Image" runat="server" /><br />
<asp:Label ID="lbl" runat="server" Text="Figure"></asp:Label><%# dlstImage.Items.Count + 1%>
</ItemTemplate>
</asp:DataList>
My both datalist is in the same webuser control, datalist1 and datalist2 and I have 2 btn up and down, when i select one image from datalist1 and click on down btn then the selected image should move to datalist2. How to do that? someone please help me,
You need to handle the ItemCommand event of one DataList in which you have to copy the selected data (image) into another dataSource of two DataList and remove that item from the datasource of one DataList.
Markup:
<asp:DataList
ID="DataList1"
runat="server"
OnItemCommand="PerformMove"
>
<ItemTemplate>
<br /><%#Eval("Text") %>
<asp:Button ID="btn1"
runat="server"
Text="Move"
CommandName="cmd"
CommandArgument='<%#Eval("Text") %>'
/>
</ItemTemplate>
</asp:DataList>
<asp:DataList ID="DataList2" runat="server">
<ItemTemplate>
<br /><%#Eval("Text") %>
</ItemTemplate>
</asp:DataList>
Code-behind (.cs)
public class Data
{
public string Text { get; set; }
public override int GetHashCode()
{
return Text.GetHashCode();
}
public override bool Equals(object obj)
{
return GetHashCode() == obj.GetHashCode();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<Data> list1 = new List<Data >()
{
new Data() { Text="One"},
new Data() { Text="Two"},
new Data() { Text="Three"},
};
List<Data> list2 = new List<Data>();
Session["list1"] = list1;
Session["list2"] = list2;
DataList1.DataSource = Session["list1"];
DataList1.DataBind();
DataList2.DataSource = Session["list2"];
DataList2.DataBind();
}
}
protected void PerformMove(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "cmd")
{
List<Data> list1 = Session["list1"] as List<Data>;
List<Data> list2 = Session["list2"] as List<Data>;
list1.Remove(new Data() { Text=e.CommandArgument.ToString() });
list2.Add(new Data() { Text = e.CommandArgument.ToString() });
DataList1.DataSource = Session["list1"];
DataList1.DataBind();
DataList2.DataSource = Session["list2"];
DataList2.DataBind();
}
}
I am using this code and its working well for me.
ArrayList ImgArry = new ArrayList();
path = objGetBaseCase.GetImages(TotImgIds);
ImgArry.Add(SelImgId);
ImgArry.Add(SelImgpath);//image name
ImgArry.Add(SelImgName);//image path
//path.Remove(ImgArry);
List<ArrayList> t = new List<ArrayList>();
if (newpath.Count > 0)
t = newpath;
t.Add(ImgArry);
newpath = t;
for (int i = 0; i < newpath.Count; i++)
{
ArrayList alst = newpath[i];
newtb.Rows.Add(Convert.ToInt32(alst[0]), alst[1].ToString(), alst[2].ToString(), i);
}
dlstSelectedImages.DataSource = newtb;
DataBind();
path = objGetBaseCase.GetImages(TotImgIds);
for (int i = 0; i < path.Count; i++)
{
ArrayList alst = path[i];
tb.Rows.Add(Convert.ToInt32(alst[0]), alst[1].ToString(), alst[2].ToString(), i);
}
dlstImage.DataSource = tb;
DataBind();

Categories