I’m currently working on an asp.net page using C#. This page contains a button and once you click you will get a small HTML table with a name of person, cell phone number and email address. What I want to do is in code behind capture this HTML table along with its data in memory stream or other type of streams in order to do some operations. Here's my code
<table id="tb" runat="server">
<tr>
<td> Name </td>
<td> <asp:Label ID="lblName" runat="server" ></asp:Label> </td>
</tr>
<tr>
<td> Phone </td>
<td> <asp:Label ID="lblPhone" runat="server" ></asp:Label> </td>
</tr>
<tr>
<td> Email </td>
<td> <asp:Label ID="lblEmail" runat="server" ></asp:Label> </td>
</tr>
</table>
So please, if anyone could help of how to accomplish this process and I will be so thankful
Well although I don't really understand why do you want to do that, you can do this pretty simply, you have to put id and runat server tag at your table, and you already have that,
and then render this control to string :
markup:
<form id="form1" runat="server">
<table id="tb" runat="server">
<tr>
<td> Name </td>
<td> <asp:Label ID="lblName" runat="server" ></asp:Label> </td>
</tr>
<tr>
<td> Phone </td>
<td> <asp:Label ID="lblPhone" runat="server" ></asp:Label> </td>
</tr>
<tr>
<td> Email </td>
<td> <asp:Label ID="lblEmail" runat="server" ></asp:Label> </td>
</tr>
</table>
<hr />
Rendered table :
<hr />
<asp:Label ID="lblRenderedTable" runat="server"></asp:Label>
<hr />
</form>
code behind:
protected void Page_Load(object sender, EventArgs e)
{
lblName.Text = "User Name";
lblEmail.Text = "user#domain.com";
lblPhone.Text = "555-4214";
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
tb.RenderControl(hw);
string tableContents = sb.ToString();
lblRenderedTable.Text = tableContents;
}
Related
I have a multiview control, that has two views. View1 is the default view, and View2 is the new view. When the end user click a button, I want to change the view to View2. I can't seem to achieve this anyway I go.
ASP.NET Code:
<asp:MultiView ID="MVOrder" runat="server">
<asp:View ID="VOrderNow" runat="server">
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<table>
<tr>
<td>
<asp:Label ID="LblInfo" runat="server"></asp:Label>
</td>
<td>
</td>
<td> </td>
</tr>
<tr>
<td>
<asp:Label ID="LblDrink" runat="server"></asp:Label>
</td>
<td></td>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="LblItemInfo" runat="server"></asp:Label>
</td>
<td>
</td>
<td> </td>
</tr>
</table>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:View>
<asp:View runat="server" ID="VOrderComplete">
<table class="auto-style1">
<tr>
<td> </td>
<td>
<asp:Label ID="LblOrderComplete" runat="server"></asp:Label>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<asp:Label ID="LblOrderNumberAgain" runat="server"></asp:Label>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<asp:Label ID="LblOrderTimeAgain" runat="server"></asp:Label>
</td>
<td> </td>
</tr>
</table>
</asp:View>
<br/>
</asp:MultiView>
Button Click Event Code:
protected void Button1_Click(object sender, EventArgs e)
{
//MVOrder.ActiveViewIndex = 1;
MVOrder.SetActiveView(VOrderComplete);
}
Page Load Code:
if (!IsPostBack)
{
MVOrder.SetActiveView(VOrderNow);
}
Can someone please tell me what I am doing wrong?
I think you might have some additional code that might make this not work properly. But based on what you've shown, this will post and update your view:
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:MultiView ID="MVOrder" runat="server">
<asp:View ID="VOrderNow" runat="server">
<table>
<tr>
<td><asp:Label ID="LblInfo" runat="server" Text="asdfasdfasdf"></asp:Label></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><asp:Label ID="LblDrink" runat="server" Text="lmnoplmnop"></asp:Label></td>
<td></td>
<td></td>
</tr>
<tr>
<td><asp:Label ID="LblItemInfo" runat="server" Text="iteminfo"></asp:Label></td>
<td> </td>
<td> </td>
</tr>
</table>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</asp:View>
<asp:View runat="server" ID="VOrderComplete">
<table class="auto-style1">
<tr>
<td> </td>
<td><asp:Label ID="LblOrderComplete" runat="server" Text="ordercomplete"></asp:Label></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><asp:Label ID="LblOrderNumberAgain" runat="server" Text="ordernumberagain"></asp:Label></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><asp:Label ID="LblOrderTimeAgain" runat="server" Text="ordertimeagain"></asp:Label></td>
<td> </td>
</tr>
</table>
</asp:View>
<br />
</asp:MultiView>
</ContentTemplate>
</asp:UpdatePanel>
The code below get me a comment , puts it in the database and then waits for a page reload to show the comment. How can i make the submited comment to be shown right after it has been posted along with the new ones ? This is the code that does the stuff :
Layout:
<table>
<tr>
<td colspan="2">All Comments</td>
<td>
 
</td>
</tr>
<tr> <td colspan="2"> </td></tr>
<tr>
<td>
<asp:Repeater ID="Repeater1" runat="server" ItemType="SiteStiri.Models.Comments" SelectMethod="GetComments" >
<ItemTemplate>
<table>
<tr>
<td colspan="2">
<asp:Label id="lblDate" runat="server" Text='<%# Item.ReleaseDate %>'></asp:Label>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="lblComment" runat="server" Text='<%# Item.Comment %>'>
</asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
<AlternatingItemTemplate>
<table>
<tr>
<td colspan="2">
<asp:Label id="Label1" runat="server" Text='<%# Item.ReleaseDate %>'></asp:Label>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="Label2" runat="server" Text='<%# Item.Comment %>'>
</asp:Label>
</td>
</tr>
</table>
</AlternatingItemTemplate>
</asp:Repeater>
</td>
</tr>
</table>
</td>
</tr>
<td>
<table>
<tr>
<td colspan="2" >Add Your Comment</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblStatus" runat="server" Visible="False"></asp:Label>
</td>
<td>
 
</td>
</tr>
<tr>
<td colspan="2">
<asp:TextBox ID="txtcomment" runat="server" TextMode="MultiLine">
</asp:TextBox>
</td>
<td>
 
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</td>
<td>
 
</td>
</tr>
</table>
</table>
</td>
</tr>
</table>
Code Behind:
public void fnSubmitComment()
{
AddComment cmt = new AddComment();
String x = Request.QueryString["newsID"].ToString();
int y = Convert.ToInt32(x);
bool addSucces = cmt.Addcomment( y , txtcomment.Text);
if (addSucces)
{
lblStatus.Text = "Your Comment has been Added Successfully.";
}
else
{
lblStatus.Text = "Your Comment has not been Added.";
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
fnSubmitComment();
}
public IQueryable<Comments> GetComments()
{
var _db = new SiteStiri.Models.CommentsContext();
IQueryable<Comments> query = _db.Comments;
String x = Request.QueryString["newsID"].ToString();
int y = Convert.ToInt32(x);
query = query.Where(p => p.NewsID == y);
return query;
}
I heard i can do that with ajax ? (My knowledge about ajax is 0 currently) Any tips on how i can do this ?
You must wrapper you repeater in an updatepanel,
set UpdateMode="Conditional" and add a trigger on btnSubmit so when click the button the update the repeater with the normal page life cycle
Remember to bind the repeater after that you add the commet in the codebehind and must be present in your page a ScriptManager control
more or less thus
<asp:ScriptManager runat="server" />
<table>
<tr>
<td colspan="2">All Comments</td>
<td> 
</td>
</tr>
<tr>
<td colspan="2"></td>
</tr>
<tr>
<td>
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater ID="Repeater1" runat="server" ItemType="SiteStiri.Models.Comments" SelectMethod="GetComments">
<ItemTemplate>
<table>
<tr>
<td colspan="2">
<asp:Label ID="lblDate" runat="server" Text='<%# Item.ReleaseDate %>'></asp:Label>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="lblComment" runat="server" Text='<%# Item.Comment %>'>
</asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
<AlternatingItemTemplate>
<table>
<tr>
<td colspan="2">
<asp:Label ID="Label1" runat="server" Text='<%# Item.ReleaseDate %>'></asp:Label>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="Label2" runat="server" Text='<%# Item.Comment %>'>
</asp:Label>
</td>
</tr>
</table>
</AlternatingItemTemplate>
</asp:Repeater>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSubmit" />
</Triggers>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td>
<table>
<tr>
<td colspan="2">Add Your Comment</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblStatus" runat="server" Visible="False"></asp:Label>
</td>
<td> 
</td>
</tr>
<tr>
<td colspan="2">
<asp:TextBox ID="txtcomment" runat="server" TextMode="MultiLine">
</asp:TextBox>
</td>
<td> 
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</td>
<td> 
</td>
</tr>
</table>
</td>
</tr>
</table>
All of my hidden fields have ID's. And in my JavaScript in trying to set the visible from false to true on the payment selection. The Cash payment should just display the address to be sent to. The credit card payment comes up with text boxes and labels in order to process the payment online. But when I run the script the wont appear with the checkbox selection. I assigned an onClick event and still doesnt work. Any suggestions?
<%# Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="ShoppingCart.aspx.cs" Inherits="ShoppingCart" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
<head>
<script type="text/javascript">
function paymentFunction() {
if (document.getElementById("rbCash").checked) {
document.getElementById("lbCash").visible = true;
document.getElementById("lbCash2").visible = true;
document.getElementById("lbCash3").visible = true;
document.getElementById("lbCash4").visible = true;
}
else
{
if(document.getElementById("rbCreditCard").checked)
{
document.getElementById("lbCard").visible = true;
document.getElementById("lbCardNum").visible = true;
document.getElementById("lbCVV").visible = true;
document.getElementById("lbexp").visible = true;
document.getElementById("ddlCard").visible = true;
document.getElementById("tbCnum").visible = true;
document.getElementById("tbcvvnum").visible = true;
document.getElementById("tbexp").visible = true;
}
}
}
</script>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 100%;
}
.style3
{
width: 130px;
}
</style>
</head>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<table class="style2">
<tr>
<td class="style3">
Payment Type:</td>
<td>
<asp:CheckBox ID="rbCash" runat="server" onClick="paymentFunction()" Text="Cash" ClientIDMode="Static"/>
<asp:CheckBox ID="rbCreditCard" runat="server" onClick="paymentFunction()" Text="Credit Card" />
</td>
</tr>
<tr>
<td class="style3">
</td>
<td>
<asp:Label ID="lbCash" runat="server" ForeColor="Black"
Text="Please Send Payment To:" Visible="False" ClientIDMode="Static"></asp:Label>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td>
<asp:Label ID="lbCash2" runat="server" ForeColor="Black"
Text="Wild Style Shoes" Visible="False" ClientIDMode="Static"></asp:Label>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td>
<asp:Label ID="lbCash3" runat="server" ForeColor="Black"
Text="1808 West Avenue" Visible="False" ClientIDMode="Static"></asp:Label>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td>
<asp:Label ID="lbCash4" runat="server" ForeColor="Black"
Text="Chicago, IL 88947" Visible="False" ClientIDMode="Static"></asp:Label>
</td>
</tr>
<tr>
<td class="style3">
<asp:Label ID="lbcard" runat="server" Text="Card Type" Visible="False"></asp:Label>
</td>
<td>
<asp:DropDownList ID="ddlCard" runat="server" Visible="False">
<asp:ListItem>Select A Card</asp:ListItem>
<asp:ListItem>Visa</asp:ListItem>
<asp:ListItem>Discover</asp:ListItem>
<asp:ListItem>MasterCard</asp:ListItem>
<asp:ListItem>American Express</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style3">
<asp:Label ID="lbcardnum" runat="server" Text="Card Number:" Visible="False"></asp:Label>
</td>
<td>
<asp:TextBox ID="tbCnum" runat="server" Visible="False" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
<asp:Label ID="lbCVV" runat="server" Text="CVV Number" Visible="False"></asp:Label>
</td>
<td>
<asp:TextBox ID="tbcvvnum" runat="server" Visible="False" Width="58px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
<asp:Label ID="lbexp" runat="server" Text="Expiration Date" Visible="False"></asp:Label>
</td>
<td>
<asp:TextBox ID="tbexp" runat="server" Visible="False"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Submit Payment" Visible="False" />
</td>
</tr>
</table>
<p>
</p>
<p>
</p>
<p>
Thank You For Shopping With Us:<br />
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
</asp:Content>
If you set Visible = false on the server side like you are doing, then the server will not render the control.
You probably want to do the following:
<asp:Label ID="lbCash" runat="server" ForeColor="Black"
Text="Please Send Payment To:" style="visibility:hidden" ClientIDMode="Static"></asp:Label>
Then in the javascript do something like this:
document.getElementById('lbCash').style.visibility = 'visible';
I want to display the values in a gridview in this way(image),how can i perform it,i have no idea,if i have to edit the column or add template fields.,please help.
If we add,a footer,it can be displayed only on the last row,but,how to make it be displayed in the center.
You would be better off using something like a Repeater or DataList control, which give you more control over the output.
<asp:GridView runat="server" ID="gdv" AutoGenerateColumns="false" Width="100%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table width="100%">
<tr>
<td>
Exam Date
</td>
<td>
<%#Eval("Exam_Date") %>
</td>
<td>
Section
</td>
<td>
<%#Eval("Section") %>
</td>
</tr>
<tr>
<td>
Total Students
</td>
<td>
<%#Eval("Total_Students") %>
</td>
<td>
No. of students passed
</td>
<td>
<%#Eval("StudentPassed") %>
</td>
</tr>
<tr>
<td colspan="2">
over all pass percentange
</td>
<td colspan="2">
<%#Eval("Overall_Percent") %>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
On cs page:
gdv.DataSource = YourDataSource;
gdv.DataBind();
I guess you need to do a column spanning to have this look. (I am assuming you have all the data needed to bind to the control.)
You can use Gridview Footer for this display .
<asp:TemplateField>
<FooterTemplate>
<table>
<tr>
<td>
<asp:Label ID="lblname" runat="server" Text="NAME"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtbx" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</FooterTemplate>
</asp:TemplateField>
On RowDataBound Event you can set your total to label .
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lbl = (Label)e.Row.FindControl("lblTotal");
lbl.Text = "Total";
}
}
i want give caption DATE,PRICE1, PRICE2 and i also show sum of price1, price2 in the footer. how can i do this?
<asp:Repeater ID="rptCari" runat="server">
<ItemTemplate>
<div>
<tr>
<td>
<asp:Label runat="server" ID="lblBelgeTarihi"><%#(Eval("DATE","{0:d}"))%></asp:Label>
</td>
<td>
</td>
<td>
<asp:Label runat="server" ID="lblAlacakTutar"><%#ValidationHelper.FormatPrice(ValidationHelper.GetDecimal(Eval("PRICE1"), 0))%></asp:Label>
</td>
<td>
</td>
<td>
<asp:Label runat="server" ID="lblBorcTutar"><%#ValidationHelper.FormatPrice(ValidationHelper.GetDecimal(Eval("PRICE2"), 0))%></asp:Label>
</td>
</tr>
</div>
</ItemTemplate>
</asp:Repeater>
You've HeaderTemplate and FooterTemplate as child elements of Repeater.