net c#. I am using a place holder, and adding span dynamically to the place holder. How can i add text to span dynamically in c# asp.net?
you can use html generic control it would be somethink like this
var yourspan = new HtmlGenericControl("span");
span.InnerHtml = "the text inside your span ";
yourspan.Attributes["id"] = "yourspan";
pannel.Controls.Add(span);
http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlgenericcontrol.aspx
Make Table runat="server"
<table id="myTable" runat="server" width="100%">
<tr>
<td>
<span></span>
</td>
</tr>
</table>
**In code behind u do like this**
myTable.Rows[1].Cells[0].InnerHtml = "<span id="1">" + YOURTEXT+ "</span>";
Related
I want to create a download list where each available file has its own ASPxButton and I would like to dynamically create them rather than creating like 30 manually and dynamically setting them to visible.
Since I don't have a form within my .aspx.cs file but create my buttons within a table in my .aspx code, I can't use the form.AddButton method. Any ideas on how to do something like that?
Not sure if that helps but here is the code for the 2 buttons I already have:
<dx:TabPage Name="Downloads" Text="Downloads" Enabled="false">
<ContentCollection>
<dx:ContentControl runat="server">
<table class="grid_centered">
<tr>
<td>
<dx:ASPxButton ID="btnDownload0" runat="server" Text="Anschreiben" Theme="Metropolis" CssClass="button centered" OnClick="btn_Click" Width="300px"></dx:ASPxButton>
</td>
</tr>
<tr>
<td>
<dx:ASPxButton ID="btnDownload1" runat="server" Text="Serie 1" Theme="Metropolis" CssClass="button centered" OnClick="btn_Click" Width="300px" Visible="false" Enabled="false"></dx:ASPxButton>
</td>
</tr>
</table>
</dx:ContentControl>
</ContentCollection>
</dx:TabPage>
The solution to this is to use an asp:Repeater. This way, I only have to write the details for one and use the OnItemCommand event to handle button clicks. I had to switch to using asp:Button instead of dx:ASPxButton, though since I needed the CommandName field to pass the info which button was pressed along to my event handler.
<asp:Repeater
ID="MyRepeater"
runat="server"
DataSourceID="MyDataSource"
OnItemCommmand="MyClickedEventHandler">
<ItemTemplate>
<asp:Button
ID="MyButton"
runat="server"
Text="Download"
CommandName='<%# $"{Eval("keyField")}" %>' />
</ItemTemplate>
</asp:Repeater>
I am not too familiar with dx:ASPxButton but I am assuming it would be similar to an aspx:Button. To dynamically create controls you have to create the controls in the Page_Load() event.
protected void Page_Load()
{
//get your files
string targetDirectory = "your directory";
string [] fileEntries = Directory.GetFiles(targetDirectory);
int i = 0;
foreach(string fileName in fileEntries)
{
ASPxButton btn = new Button();
btn.Text = fileName;
btn.ID = "btnDownload" + i.ToString();
btn.CssClass = "button centered";
btn.Attributes.Add("Theme", "Metropolis");
//add any other properties you need
//add the event handler
btn.Click += new EvenHandler(btn_Clik);
form.AddButton(btn);
}
}
I have an ASP.NET WebForms project that displays data read from a database via a Repeater. The code for the repeater looks like this:
<asp:Repeater ID="repRMAproduct" runat="server">
<ItemTemplate>
<tr>
<td>
<%# Eval("Description") %>
</td>
<td>
<%# Eval("Qty") %>
</td>
<td>
<asp:TextBox ID="tbNewQty" runat="server"></asp:TextBox>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
What I want is for the user to enter a new Quantity only on the rows that he wants to enter. Then, when he clicks the save button, I can read these new Quantities along with their associated Descriptions.
In MVC I should build the URL using jQuery and pass it to the Controller. But in WebForms it seems less intuitive. Where do I go from here?
In your save-button-click event handler you can loop all items and get the TextBox with FindControl:
foreach(RepeaterItem item in repRMAproduct.Items)
{
TextBox tbNewQty = (TextBox) item.FindControl("tbNewQty");
string newQuantity = tbNewQty.Text.Trim();
int quantity;
if(int.TryParse(newQuantity, out quantity))
{
SaveNewRmaQuantity(product, quantity);
};
}
If you'd use a Label for your product-description you can use FindControl("LblDescriptionId") to get it and to pass it's Text to SaveNewRmaQuantity. You can also use invisible controls(Visible=false), f.e. a Label, to store the ProductId.
I have a Table with multiple rows. These rows contain an asp.net multirow textbox
<table>
<tr>
<td>Invoice 1</td>
<td><asp:TextBox ID="invoiceDesc" runat="server" Rows="1" TextMode="MultiLine"></asp:TextBox></td>
</tr>
<tr>
<td>Invoice 2</td>
<td><asp:TextBox ID="invoiceDesc" runat="server" Rows="1" TextMode="MultiLine"></asp:TextBox></td>
</tr>
<tr>
<td>Invoice 3</td>
<td><asp:TextBox ID="invoiceDesc" runat="server" Rows="1" TextMode="MultiLine"></asp:TextBox></td>
</tr>
</table>
The table is rendered using an asp.net gridview control. I'm trying to adjust each textbox number of rows depending on the content loaded.
So far I've tried adding the code below to $(document).ready :
var text = $("#<%=invoiceDesc.ClientID%>").val();
var lines = text.split(/\r|\r\n|\n/);
var count = lines.length;
$('#<%=invoiceDesc.ClientID%>').attr('rows', count);
But the jQuery Code above doesn't work.
Replace the javascript code given below and pass your textarea id instead of test that i have used as sample.
var count = 20; //set the cols attribute of the textarea here i have set it to 20 just for an example which is by default for HTML textarea
var lineCount = ($('#test').val().length / count);
var lineBreaksCount = ($('#test').val().split('\r\n'));
alert(Math.round(lineCount)+1);
I hope this will solve your problem :)
I've got this code in the code-behind on my page, which works perfectly fine for a repeater:
protected void AcctAssnRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["PBRConnectionString"].ConnectionString);
con.Open();
{
string str10 = ((HtmlInputText)e.Item.FindControl("txtFlgUpdatedOn")).Value;
}
}
I'm trying to do something similar on another page, but it is telling me that "item" isn't valid:
protected void btnSubmit_OnClick(object sender, EventArgs e)
{
string str10 = ((HtmlInputText)e.Item.FindControl("txtDtSentToCIS")).Value;
}
I'm still a C# n00b, can anyone tell me how to reference the value inside this control? Both pages have a runat="server" on the aspx side, so I would expect that there's a way to do it, I'm sure my syntax just needs adjusting.
Thanks!
EDIT: Here's a piece of the aspx. Maybe I should note that it's all inside a Tab Control.
<div style="width:430px;border:1px solid blue;float:left;">
<asp:Panel ID="Panel3" runat="server" Height="220px" style="float:left; margin-left: 19px"
Width="410px">
<table>
<tr>
<td width="210px">BIL Name:</td>
<td width="200px"><asp:textbox id="txtCISName" runat="server"></asp:textbox></td>
</tr>
<tr>
<td width="210px">Date Sent To BIL:</td>
<td width="200px"><input type="text" id="txtDtSentToCIS" class="datepicker" name="txtDtSentToCIS" runat="server" style="height: 14px; width: 70px" /></td>
</tr>
<tr>
<td width="210px">BIL Sign Off Received:</td>
<td width="200px"><asp:DropDownList ID="cboCISSignOff" runat="server" Height="16px"
AutoPostBack="True" onselectedindexchanged="chkCISSignOff_CheckedChanged">
<asp:ListItem>N</asp:ListItem>
<asp:ListItem>Y</asp:ListItem>
</asp:DropDownList></td>
</tr>
<tr>
<td width="210px"><asp:Label runat="server" Text="BIL Response:" ID="CISResp" /></td>
<td width="200px"><asp:textbox id="txtCISResponse" runat="server"
textmode="MultiLine" rows="9" Width="180px"></asp:textbox></td>
</tr>
</table>
</asp:Panel>
</div>
You should be able to use the ID to reach the control. For ex. a textbox:
<asp:TextBox ID="txtDtSentToCIS" runat="server" />
In your code behind you can do something as
SomeMethod(this.txtDtSentToCIS.Text);
or
string enteredByUser = this.txtDtSentToCIS.Text;
If you want to access repeater containing textbox(es) from outside the repeater, you may code like this:
Iterating the RepeaterItemCollection:
foreach (RepeaterItem item in Repeater1.Items)
{
TextBox txtDtSentToCIS = item.FindControl("txtDtSentToCIS") as TextBox;
}
Or simply accessing through indexer:
TextBox txtDtSentToCIS = Repeater1.Items[1].FindControl("txtDtSentToCIS") as TextBox;
Good luck.
If you are using asp I would suggest that you use an asp Text box.
<asp:TextBox ID="textBoxName" runat="server" />
When the button is clicked and you OnClick method is called you can search for the text box by name and assign the text to your string.
string str10 = textBoxName.text;
You don't really need to use the find control unless that control is buried in another control like a grid view or login view.
What could be happening is that the FindControl method only works for the first control collection in this case the only control contained is "Panel3", try this:
var panel = e.Item.FindControl("Panel3") as Panel;
string str10 = ((HtmlInputText)panel.FindControl("txtFlgUpdatedOn")).Value;
Good luck
I have a repeater control:
<table style="width: 100%">
<asp:Repeater runat="server" ID="rptOptions" OnItemDataBound="rptOptions_ItemDataBound">
<HeaderTemplate>
<tr>
<td class="GridHeader">Account</td>
<td class="GridHeader">Margin</td>
<td class="GridHeader">Symbol</td>
<td class="GridHeader">Price</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="GridRow"><asp:Label runat="server" ID="lblOptionAccount"></asp:Label></td>
<td class="GridRow"><asp:Label runat="server" ID="lblOptionMargin"></asp:Label></td>
<td class="GridRow"><asp:Label runat="server" ID="lblOptionSymbol"></asp:Label></td>
<td class="GridRow"><asp:Label runat="server" ID="lblOptionPrice"></asp:Label></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
And the following code-behind databound method:
protected void rptOptions_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Option rowOption = (Option)e.Item.DataItem;
((Label)e.Item.FindControl("lblOptionAccount")).Text = rowOption.Account;
((Label)e.Item.FindControl("lblOptionMargin")).Text = rowOption.Margin ? "Y" : "N";
((Label)e.Item.FindControl("lblOptionSymbol")).Text = rowOption.Symbol;
((Label)e.Item.FindControl("lblOptionPrice")).Text = rowOption.Price.ToString("C", currencyFormat);
}
}
There are more columns in that grid, but I've slimmed it down just for the question.
Now, what I would like to do is change the tr's background color based on the price amount. If it is within different levels, I would like to change the rows background color correspondingly.
Do I have to do this with javascript or is there some way I can get access to the table rows in the code-behind to set this color?
make it to runat="Server"
<tr runat="server" ID="trHeader"></tr>
Then find that Table Row in by ID in your code behind in databound event, like what you are doing for other server side control and change color.
another option:
<tr class="<%# GetClassForPrice( Container.DataItem ) %>">
and in code-behind
protected string GetClassForPrice( object data )
{
var rowOption = (Option)data;
if(rowOption.Price > 100) return "red";
else return "green";
}
also... any reason you're not using data binding? it would let you eliminate your ItemDataBound code-behind method.
<tr>
<td class="GridRow"><%# Eval("Account") %></td>
<td class="GridRow"><%# ((bool)Eval("Margin")) ? "Y" : "N" %></td>
<td class="GridRow"><%# Eval("Symbol") %></td>
<td class="GridRow"><%# Eval("Price", "{0:c}") %></td>
</tr>
use this code inside repeter databound event.
HtmlTableRow tr = (HtmlTableRow)e.Item.FindControl("trID");
tr.Visible = false;
Another option you can use is jQuery. Seeing you're using a repeater, that gives you full control over your output. Jquery can get into your table, look at the data and format it how you want it.
Look at:
http://plugins.jquery.com/project/Plugins/category/54
http://plugins.jquery.com/project/Colorize
http://franca.exofire.net/js/demo_cross.html
Alternately you can use the Code behind in the repeater to set the CSS Class of the cells/rows/columns you need changed.
You'll need to make those controls server controls (require: ID="my_thing" runat="server"), create those controls (use the find control and bind them), then set the CSS class after you determine the value.
Dave Thieben's answer works nicely, except that the css is missing.
If you add the following then the example will work:
table tr.red td { background-color:red; }
table tr.green td { background-color:green; }