I use asp checkboxlist to have that result
But using html tag as item of the checkboxlist , asp is interpreting it as html. It works for simple text. Here is my result.
and here is the declaration and binding method
<asp:CheckBoxList ID="chklstreponse" runat="server">
</asp:CheckBoxList>
DataTable dtreponse = gq.GetRandom_Responses(Convert.ToInt32(idquest.Value));
chkList.DataSource = dtreponse;
chkList.DataTextField = "libelle";
chkList.DataValueField = "id";
chkList.DataBind();
I think you need to HtmlEncode the values in the RadioButtonList.
System.Net.WebUtility.HtmlEncode("<html>")
But you are binding a datatable directly, you either must do it in the source of the DataTable or loop all rows and encode them.
foreach (DataRow row in dtreponse.Rows)
{
row["libelle"] = System.Net.WebUtility.HtmlEncode(row["libelle"].ToString());
}
chkList.DataSource = dtreponse;
chkList.DataTextField = "libelle";
chkList.DataValueField = "id";
chkList.DataBind();
Try this.HtmlEncode makes sure that text is displayed correctly in the browser and not interpreted by the browser as HTML.
<asp:CheckBoxList ID="chklstreponse" runat="server"> </asp:CheckBoxList>
DataTable dtreponse = gq.GetRandom_Responses(Convert.ToInt32(idquest.Value));
chkList.DataSource = dtreponse;
chkList.DataTextField = Server.HtmlEncode("libelle");
chkList.DataValueField = "id";
chkList.DataBind();
Related
I need a bunch of GridViews to display on my page. I have a List of Lists of Section Objects called dataList, and each List in dataList should be bound to one of the GridViews.
I have this
List<List<Section>> dataList;
foreach (List<Section> sectionList in dataList)
{
GridView gv = new GridView();
gv.DataSource = sectionList
gv.DataBind();
}
and my markup:
<asp:GridView runat="server" ID="gv" AutoGenerateColumns="true"/>
but when I load the page I can't see anything. How do I display all of the GridViews I created? Does the foreach loop I have successfully bind each GridView to the Lists of dataList? Thanks
Remove your Gridview markup and Use PlaceHolder control to add dynamic Gridviews, Try this:
<asp:PlaceHolder ID="PlaceHolder1" runat="server"/>
in C#
int i = 1;
foreach (List<Section> sectionList in dataList)
{
GridView gv = new GridView();
//generate dynamic id
gv.Id = "gv" + i; i++;
gv.AutoGenerateColumns="true";
gv.DataSource = sectionList
gv.DataBind();
PlaceHolder1.Controls.Add(gv);
}
There is a similar thread about this. But I want to have a multiline TextBox with automatic width (fit width to the larger row).
With this code I can have a multiline TextBox (automatic height)
<div style="float:left; white-space:nowrap ">
<asp:TextBox style="display:inline; overflow:hidden"
ID="txt1"
runat="server"
Wrap="false"
ReadOnly="true"
TextMode="MultiLine"
BorderStyle="none"
BorderWidth="0">
</asp:TextBox>
</div>
<div style="float:left">
<asp:TextBox ID="txt2" runat="server" Text="Example textbox"></asp:TextBox>
</div>
Code behind:
txt1.Rows= text.Split("|").Length ' Sets number of rows but with low performance
txt1.Text = text.Replace("|", Environment.NewLine)
Once again, thanks for your help.
You could try a linq approach:
string[] rows = text.Split('|');
int maxLength = rows.Max(x => x.Length);
txt1.Rows = rows.Length;
txt1.Columns = maxLength;
If you are open to using plugins like jquery,
you should look at autoresize plugins.
These will resize as a user types too.
Check out one autoresize
$(document).ready(function(){
$('textarea').autosize();
});
Joel Etherton give me a real good working code example of how to resolve this using Linq, but unfurtenly I can't use Linq.
Multiline TextBox auto Width using Linq (Joel Etherton's solution):
C#
string[] rows = text.Split('|');
int maxLength = rows.Max(x => x.Length);
txt1.Rows = rows.Length;
txt1.Columns = maxLength;
VB
Dim rows() As String = text.Split("|")
Dim maxLength As Integer = rows.Max(Function(x) x.Length)
txt1.Rows = rows.Length
txt1.Columns = maxLength
text = text.Replace("|", Environment.NewLine)
txt1.Text = text
Multiline TextBox auto Width solution 2
To achieve this "manually", I did this method to know the larger row's lenght. Is not the most efficiente one, but It worked for me:
Dim textRows() As String = text.Split("|")
For Each row As String In textRows
row = row.Trim
textToDisplay = String.Format("{0}{1}{2}", textToDisplay, row, Environment.NewLine)
If row.Length > maxRowLenght Then
maxRowLenght = row.Length
End If
Next
txt1.Rows = textRows.Length
txt1.Columns = maxRowLenght
txt1.Text = textToDisplay
I am adding columns to RadGrid from code behind. In NeedDataSource event, I am binding a DataTable(with 10 columns) to the radgrid.
Everything's working well till here. But I would like to have text boxes in 2 columns(on load itself, not just in edit mode).
<telerik:RadGrid ID="RadGrid1" runat="server" ShowHeader="true"
OnNeedDataSource="RadGrid1_NeedDataSource" OnPreRender="RadGrid1_PreRender"
AutoGenerateColumns="true" >
<MasterTableView>
</MasterTableView>
</telerik:RadGrid>
If done declarative, the column definition shall be like this. But I want it accomplished from code behind.
<telerik:GridTemplateColumn HeaderText="Qty">
<ItemTemplate>
<input id="<%# this.GetUniqueId("Qty", Container.DataItem)%>" name="<%# this.GetUniqueId("Qty", Container.DataItem)%>" type="text" value="<%# Eval("Quantity")%>" size="2" maxlength="3" />
</ItemTemplate>
</telerik:GridTemplateColumn>
Create TemplateColumn like any other column type and set template object to ItemTemplate (and you can do same for HeaderTemplate and FooterTemplate). But you have to define custom template class witch will implement ITemplate.
You can find an example here :
http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html#Section4
Must you use a template column? If you can directly bind your columns to a datasource field, use the GridNumericColumn. This can be dynamically added from the code behind:
GridNumericColumn numColumn = new GridNumericColumn();
numColumn.UniqueName = "ColumnId";
numColumn.MaxLength = 20;
numColumn.HeaderText = "My Numeric Column";
numColumn.DataField = "Qty";
numColumn.DataFormatString =
myGrid.MasterTableView.Columns.Add(numColumn);
Use This.
GridTemplateColumn tempCol;
for (int i = 0; i < obj.Count; i++)
{
tempCol = new GridTemplateColumn();
this.gvwRejection.MasterTableView.Columns.Add(tempCol);
tempCol.ItemTemplate = new DynamicTemplateCoulmn"txtCategoryQty"+ , "numericTextBox");
tempCol.HeaderText = objRejectionCategoryMasterObject[i].CategoryName.Trim();
tempCol.UniqueName = "CategoryQty" + i;
tempCol.HeaderStyle.HorizontalAlign = HorizontalAlign.Right;
tempCol.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
tempCol.FooterStyle.HorizontalAlign = HorizontalAlign.Right;
RejCategoryIDs[i] = objRejectionCategoryMasterObject[i].RejCategoryID;
}
tempCol = new GridTemplateColumn();
this.gvwRejection.MasterTableView.Columns.Add(tempCol);
tempCol.ItemTemplate = new DynamicTemplateCoulmn("txtTotal", "numericTextBoxReadOnly");
tempCol.HeaderText = "Total";
tempCol.UniqueName = "Total";
tempCol.HeaderStyle.HorizontalAlign = HorizontalAlign.Right;
tempCol.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
tempCol.FooterStyle.HorizontalAlign = HorizontalAlign.Right;
I need to hide /unhide the labels and textbox depending on db results , I tried something like this but it doesnt works, the condition should be like if the db field is empty for that field , then the label associated with that field should hidden (not visible) , following is the code i tried :
<asp:Label ID="lblBirth" Text="DOB:" runat="server" ViewStateMode="Disabled" CssClass="lbl" />
<asp:Label ID="DOB" runat="server" CssClass="lblResult" Visible='<%# Eval("Berth") == DBNull.Value %>'></asp:Label>
Code behind:
protected void showDetails(int makeID)
{// get all the details of the selected caravan and populate the empty fields
DataTable dt = new DataTable();
DataTableReader dtr = caravans.GetCaravanDetailsByMakeID(makeID);
while (dtr.Read())
{
//spec
string value = dtr["Price"].ToString();
lblModel.Text = dtr["model"].ToString();
birthResult.Text = dtr["Berth"].ToString(); }}
To make your aspx version work your control should be data bound to data source that contains "Berth" property. As I can see from code behind, you prefer to use c# to populate controls. In this case you may just do the following:
DOB.Visible = dtr["Berth"] == DBNull.Value;
I think that using data binding is more preferable solution.
I am trying to display contents of a folder in a hyperlink. I am using masterpage also. The hyperlinks are not shown into the content page. what to do for that?
I know in windows forms we can use like
TextBox.Location=new Point(100,100);
But how to do in web page...please anybody suggest me..
my coding in page_load is
protected void Page_Load(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/ProjectsUpload"));
int i = 0;
foreach (FileInfo fi in di.GetFiles())
{
HyperLink HL = new HyperLink();
HL.ID = "HyperLink" + i++;
HL.Text = fi.Name;
HL.NavigateUrl = "downloading.aspx?file=" + fi.Name;
Page.Controls.Add(HL);
Page.Controls.Add(new LiteralControl("<br/>"));
}
}
You can't add it directly to Page.Controls. You have to add it to the ContentPlaceHolder on the page.
Instead of dynamically creating controls, which is rather messy and error-prone, have you considered using an asp:Repeater control and binding the files directly to it? Something like:
<asp:Repeater ID="RepeaterFiles" runat="server">
<ItemTemplate>
<asp:HyperLink runat="server" Text='<%# Container.DataItem %>'
NavigateUrl='<%# String.Format("downloading.aspx?file={0}", Container.DataItem)%>' />
<br />
</ItemTemplate>
</asp:Repeater>
and in code behind:
DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/ProjectsUpload"));
RepeaterFiles.DataSource = di.GetFiles();
RepeaterFiles.DataBind();
That way you can use declarative mark-up to control layout and keep the logic in your code-behind.
Put a PlaceHolder control on your page:
<asp:PlaceHolder runat="server" ID="ph" />
In your code behind write like:
HyperLink HL = new HyperLink();
HL.ID = "HyperLink" + i++;
HL.Text = fi.Name;
HL.NavigateUrl = "downloading.aspx?file=" + fi.Name;
ph.Controls.Add(HL);
ph.Controls.Add(new Literal { Text = "<br/>"});
I'm making use of the newly C# 3 feature on that last line to set the Text property.
Have you used the debugger to step through the loop to verify that it processes at least one file?
Instead of adding the links to Page.Controls, you could put a list control on the page, and then add each link within a list item. Then you'd know precisely where on the page they should appear.
Create a Panel or a Label in the Page's Content area, and add your HyperLinks into the Controls collection of the Panel.
(Stepping through the code to check whether the IIS App actually enumerates any files in the directory would help too.)