I cant seem to pass my variable through to the next page. Can anyone tell me what i am doing wrong?
var finalValue = value * sqlCouponValue;
finalValue = Math.Round(finalValue, 2);
Session["discountedOrderTotal"] = finalValue.ToString();
where i am trying to call it again on the next page:
e.Row.Cells[4].Text = "$" + Session["discountOrderTotal"];
Does anyone have any ideas? I have never used session variables before and am not sure why it is just returning a $. Any help would be much appreciated. Thanks!
you have different names. discountedOrderTotal vs discountOrderTotal
The first thing you should be careful is the name of the session. It should be same and when you are retrieving the session then you need to specify the type because it returns an object. So try this when you call this to next page
e.Row.Cells[4].Text = "$" + Session["discountedOrderTotal"].ToString();
public partial class Approve : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Convert.ToString(Session["id"]);
Label3.Text = Convert.ToString(Session["name"]);
}
protected void Button1_Click(object sender, EventArgs e)
{
int id1=Convert.ToInt32(Session["id"]);
TransferDAL dalob = new TransferDAL();
int x = dalob.updateapprove(id1);
Response.Redirect("View.aspx");
}
protected void Button2_Click(object sender, EventArgs e)
{
int id1 = Convert.ToInt32(Session["id"]);
TransferDAL dalob = new TransferDAL();
int r = dalob.updatereject(id1);
Response.Redirect("View.aspx");
}
}
public partial class View : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
List<TransferBO> List2 = new List<TransferBO>();
TransferDAL obj1 = new TransferDAL();
List2 = obj1.view();
Gridview1.DataSource = List2;
Gridview1.DataBind();
}
}
protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow SelectedRow = Gridview1.SelectedRow;
Label id1 = (Label)SelectedRow.FindControl("Label1") as Label;
int id2 = Convert.ToInt32(id1.Text);
Label id3 = (Label)SelectedRow.FindControl("Label3") as Label;
string id4 = Convert.ToString(id3.Text);
Session["id"] = id2;
Session["name"] = id4;
Response.Redirect("Approve.aspx");
}
}
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Table ID="Table1" runat="server">
<asp:TableRow>
<asp:TableCell>Transfer Request ID</asp:TableCell>
<asp:TableCell> <asp:Label ID="Label1" runat="server" Text='<%# Eval("TransferRequestId") %>'></asp:Label></asp:TableCell>
<asp:TableCell></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Employee Name</asp:TableCell>
<asp:TableCell> <asp:Label ID="Label3" runat="server" Text='<%# Eval("EmployeeName") %>'></asp:Label></asp:TableCell>
<asp:TableCell></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell><asp:Button ID="Button1" runat="server" Value="APPROVE" Text="Approve" OnClick="Button1_Click"></asp:Button></asp:TableCell>
<asp:TableCell><asp:Button ID="Button2" runat="server" value="REJECT" Text="Reject" Onclick="Button2_Click"></asp:Button></asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Content>
Related
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);
}
}
}
}
}
Result:
I am working on a project in which I am to display, insert, delete the data in my access database. I made buttons that will display a specific table from the database. I then made an asp column to display the delete button next to each row. The issue that I am trying to figure out is: How can I have it so that when the delete button is clicked the specific row is identified so then I may delete it? Any hints or tips are welcomed. Thank you.
<body>
<h1 class="center" style="text-align: center" >Display, Delete, and Add</h1>
<h3 id="Title1"></h3>
<style>
.center{
margin: 0 auto;
}
</style>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" class="center" runat="server" Width="300px" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn1" Text="Delete" runat="server" OnClick="btn1_Click"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<p>
</p>
<p >
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Text="Courses" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Student Information" />
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Students" />
</p>
<p>
</p>
<p>
</p>
<p>
</p>
</form>
public partial class _Default : System.Web.UI.Page
{
OleDbConnection con = new OleDbConnection (#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\achowdhary\Documents\Database1.accdb");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
OleDbDataAdapter ada = new OleDbDataAdapter(" SELECT * FROM COURSES", con);
DataSet set = new DataSet();
ada.Fill(set, "COURSES");
DataTable tab = new DataTable();
tab = set.Tables["COURSES"];
GridView1.DataSource = tab;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
OleDbDataAdapter ada = new OleDbDataAdapter(" SELECT * FROM STUDENT_INFO", con);
DataSet set = new DataSet();
ada.Fill(set, "STUDENT_INFO");
DataTable tab = new DataTable();
tab = set.Tables["STUDENT_INFO"];
GridView1.DataSource = tab;
GridView1.DataBind();
}
protected void Button3_Click(object sender, EventArgs e)
{
OleDbDataAdapter ada = new OleDbDataAdapter(" SELECT * FROM STUDENTS", con);
DataSet set = new DataSet();
ada.Fill(set, "STUDENTS");
DataTable tab = new DataTable();
tab = set.Tables["STUDENTS"];
GridView1.DataSource = tab;
GridView1.DataBind();
}
protected void btn1_Click(object sender, EventArgs e)
{
//ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + "hello" + "');", true);
}
}
Many possibilites, my prefer is, bind a commandArgument in button with your identifier, and recover this value on post back
protected void btn1_Click(object sender, EventArgs e)
{
var identifier = ((Button)sender).CommandArgument;
}
Html
<ItemTemplate>
<asp:Button ID="btn1" Text="Delete" runat="server" OnClick="btn1_Click" CommandArgument='<%# Eval("PropertyName") %>'/>
</ItemTemplate>
I have a richtextbox and a gridview.
When I enter the data into the richtextbox, it should be displayed in a gridview and saved in database.
Now my requirement is that, if i am entering a paragraph or a large amount of data I should display a "readmore" button, that when clicked, display the complete data.
<%# Register Assembly="FreeTextBox" Namespace="FreeTextBoxControls" TagPrefix="FTB" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Richtextbox Sample</title>
<script type="text/javascript">
function validate() {
var doc = document.getElementById('FreeTextBox1');
if (doc.value.length == 0) {
alert('Please Enter data in Richtextbox');
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<FTB:FreeTextBox ID="FreeTextBox1" runat="server">
</FTB:FreeTextBox>
</td>
<td valign="top">
<asp:GridView runat="server" ID="gvdetails" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="RichtextBoxData">
<ItemTemplate>
<asp:Label ID="lbltxt" runat="server" Text='<%#Bind("RichtextData") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
<asp:Button ID="btnSubmit" runat="server" OnClientClick="return validate()"
Text="Submit" onclick="btnSubmit_Click" />
<br />
<asp:Label ID="lbltxt" runat="server"/>
</form>
</body>
</html>
c# code-
SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindGridview();
}
protected void BindGridview()
{
con.Open();
SqlCommand cmd = new SqlCommand("select RichTextData from RichTextBoxData", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvdetails.DataSource = ds;
gvdetails.DataBind();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into RichTextBoxData(RichTextData) values(#Richtextbox)", con);
cmd.Parameters.AddWithValue("#Richtextbox", FreeTextBox1.Text);
cmd.ExecuteNonQuery();
con.Close();
FreeTextBox1.Text = "";
BindGridview();
}
first in your select query add id of your text(your primary key of table RichTextBoxData)
and in gridview make it,s visible =false like this
<asp:TemplateField HeaderText="id" InsertVisible="False" Visible="False">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
and then
protected void gvdetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lb =e.Row.FindControl("lbltxt") as Label;
if (lb.Text.Length > 15)//any length u want
{
DataRow drv = ((DataRowView)e.Row.DataItem).Row;
int tempID = Convert.ToInt32(drv["id"].ToString());
HyperLink hp = new HyperLink();
hp.Text = "read more";
hp.NavigateUrl = "~/mydetails.aspx?id=" + tempID;
e.Row.Cells[1].Controls.Add(hp);
lb.Text = lb.Text.Substring(0, 15);
}
}
}
and on page_load of mydetails.aspx make
query to select RichTextData where id=request.querystring["id"]
i want to findcontrol of Footertemplate in DataGrid Asp . But it return null . Can you help me .
<asp:DataGrid ID="dtgDSSP" runat="server" AutoGenerateColumns="false" OnItemDataBound="dtgDSSP_ItemDataBound"
ShowFooter="true" onselectedindexchanged="dtgDSSP_SelectedIndexChanged">
<Columns>
<asp:TemplateColumn HeaderText="Sản Phẩm">
<ItemTemplate>
<asp:HiddenField ID="HidIDSP" runat="server" />
<asp:DropDownList ID="dropSanPham" runat="server">
</asp:DropDownList>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="dropSPAdd" runat="server">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateColumn>
Code behind :
protected void dtgDSSP_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList dropAdd;
int footerIndex = dtgDSSP.Controls[0].Controls.Count - 1;
dropAdd = dtgDSSP.Controls[0].Controls[footerIndex].FindControl("dropSPAdd") as DropDownList;
if (dropAdd != null)
{
dropAdd.DataSource = Constant.dictSanPham;
dropAdd.DataValueField = "key";
dropAdd.DataTextField = "value";
dropAdd.DataBind();
}
}
Get Footer dropSPAdd control with below syntex:
dtgDSSP.Controls[0].Controls[dtgDSSP.Controls[0].Controls.Count - 1].Controls[0].FindControl("dropSPAdd") as DropDownList
So Check below code
Html :-
<asp:DataGrid ID="dtgDSSP" runat="server" AutoGenerateColumns="false" OnItemDataBound="dtgDSSP_ItemDataBound"
ShowFooter="true" OnSelectedIndexChanged="dtgDSSP_SelectedIndexChanged">
<Columns>
<asp:TemplateColumn HeaderText="Sản Phẩm">
<ItemTemplate>
<asp:HiddenField ID="HidIDSP" runat="server" />
<asp:DropDownList ID="dropSanPham" runat="server">
</asp:DropDownList>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="dropSPAdd" runat="server">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="Select" CommandName="Select"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
Code Behind:-
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
System.Data.DataTable dtobj = new System.Data.DataTable();
dtobj.Columns.Add("Test");
dtobj.Rows.Add();
dtobj.Rows[dtobj.Rows.Count - 1]["Test"] = "Testimg";
dtobj.Rows.Add();
dtobj.Rows[dtobj.Rows.Count - 1]["Test"] = "Testimg111";
dtgDSSP.DataSource = dtobj;
dtgDSSP.DataBind();
}
}
protected void dtgDSSP_SelectedIndexChanged(object sender, EventArgs e)
{
var dropAdd = dtgDSSP.Controls[dtgDSSP.Controls.Count - 1].Controls[dtgDSSP.Controls[0].Controls.Count - 1].Controls[0].FindControl("dropSPAdd") as DropDownList;
if (dropAdd != null)
{
dropAdd.DataSource = Constant.dictSanPham;
dropAdd.DataValueField = "key";
dropAdd.DataTextField = "value";
dropAdd.DataBind();
}
}
protected void dtgDSSP_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
var dropAdd = e.Item.Controls[0].FindControl("dropSPAdd") as DropDownList;
if (dropAdd != null)
{
dropAdd.DataSource = Constant.dictSanPham;
dropAdd.DataValueField = "key";
dropAdd.DataTextField = "value";
dropAdd.DataBind();
}
}
}
In my <asp:Repeater></asp:Repeater> - There is one hidden field, textbox and button.
When data will be bound, then hidden filed will get User Id like below code
<asp:HiddenField ID="hide" Value='<%#Eval("UserId")%>' runat="server"/>
and ItemDataBound event will be called and in this function, i am getting value from hidden field and concatenating that value as a Id in text box. like below code
TextBox txt = (TextBox)e.Item.FindControl("txtReplyArea");
HiddenField hf = (HiddenField)e.Item.FindControl("hide");//1
txt.ID = "txtReplyArea" + hf.Value;//txtReplyArea1
Suppose only one record comes from database and their UserId is 1. Then textbox Id should be "textReplyArea1". From now, all are correct.
I am not sure, it's correct way to giving dynamic Id to repeater control but i think, it's correct.
Problem -
When i click on button and i get a items from repeater and textbox from Id by finding control from repeater then it shows null.
int areaId = int.Parse((sender as Button).CommandArgument);//1
string id="txtReplyArea"+areaId;//txtReplyArea1
foreach (RepeaterItem item in repeaterBlog.Items)
{
TextBox tb = item.FindControl(id) as TextBox;//tb = null
}
Code of aspx page
<%# Page Title="Messages" Language="C#" MasterPageFile="~/Menu.master" AutoEventWireup="true" CodeFile="Messages.aspx.cs" Inherits="Messages" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div style="width:70%;">
<asp:Repeater ID="repeaterBlog" runat="server" OnItemDataBound="repeaterBlog_ItemDataBound">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr >
<td>
<asp:HiddenField ID="hide" Value='<%#Eval("UserId")%>' runat="server"/>
<asp:TextBox ID="txtReplyArea" runat="server" TextMode="Multiline" Columns="70" Rows="8" Visible="false"></asp:TextBox>
</td>
</tr>
<tr>
<td style="margin-left:47%;">
<asp:Button ID="btnReply" runat="server" Text="Reply" OnClick="btnReplyClicked" AutoPostBack="True" CommandArgument='<%#Eval("UserId")%>'/>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</asp:Content>
Code of aspx.cs
SqlCommand cmd;
SqlDataReader sdr;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
String cs = ConfigurationManager.ConnectionStrings["myWebsite"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
cmd = new SqlCommand("select * from ContactMessage", con);
con.Open();
sdr = cmd.ExecuteReader();
repeaterBlog.DataSource = sdr;
repeaterBlog.DataBind();
con.Close();
}
}
}
public void repeaterBlog_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
TextBox txt = (TextBox)e.Item.FindControl("txtReplyArea");
HiddenField hf = (HiddenField)e.Item.FindControl("hide");
txt.ID = "txtReplyArea" + hf.Value;
}
}
protected void btnReplyClicked(object sender, EventArgs e)
{
int areaId = int.Parse((sender as Button).CommandArgument);
string id="txtReplyArea"+areaId;
foreach (RepeaterItem item in repeaterBlog.Items)
{
TextBox tb = item.FindControl(id) as TextBox;
}
}
}
This worked for me. Notice that I removed EnableViewState from the repeater and added OnItemDatabound event.
<asp:Repeater ID="repeaterBlog" runat="server" OnItemDataBound="repeaterBlog_ItemDataBound">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:HiddenField ID="hide" Value='<%#Eval("UserId")%>' runat="server" />
<asp:TextBox ID="txtReplyArea" runat="server" TextMode="Multiline" Columns="70" Rows="8"
Visible="false"></asp:TextBox>
</td>
</tr>
<tr>
<td style="margin-left: 47%;">
<asp:Button ID="btnReply" runat="server" Text="Reply" OnClick="btnReplyClicked" AutoPostBack="True"
CommandArgument='<%#Eval("UserId")%>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("UserId");
DataRow dr = dt.NewRow();
dr[0] = 34;
dt.Rows.Add(dr);
repeaterBlog.DataSource = dt;
repeaterBlog.DataBind();
}
public void repeaterBlog_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
TextBox txt = (TextBox)e.Item.FindControl("txtReplyArea");
HiddenField hf = (HiddenField)e.Item.FindControl("hide");
txt.ID = "txtReplyArea" + hf.Value;
}
}
protected void btnReplyClicked(object sender, EventArgs e)
{
int areaId = int.Parse((sender as Button).CommandArgument);
string id = "txtReplyArea" + areaId;
foreach (RepeaterItem item in repeaterBlog.Items)
{
TextBox tb = item.FindControl(id) as TextBox;
}
}
You can try like this
<asp:TextBox ID='<%# "txtReplyArea" + Convert.ToString(Eval("UserId")%>)' runat="server" TextMode="Multiline" Columns="70" Rows="8" Visible="false"></asp:TextBox>