Vinit Patel

Archive for the ‘Asp.Net’ Category

If you have CheckboxList like

0 – 5

5 – 10

10 – 15

15 – 20

25+

If you had selected multiple like 5 – 10 & 10 – 15 then Values be like 5 – 15.

So, Here is the code for that,

protected void btn1_Click(object sender, EventArgs e)
{
String values = “”;
for (int i = 0; i < cbl1.Items.Count; i++)
{
if (cbl1.Items[i].Selected)
{
values += cbl1.Items[i].Value + “,”;
}
}
values = values.TrimEnd(‘,’);
lbl1.Text = Combine(values);
}

 

//Function which actually returns the combine values

public string Combine(string input)
{
//First remove all the white spaces
string result = input.Replace(” “, string.Empty);
//Split on each ,
string[] results = result.Split(‘,’);
//Now split on each –
List<string> final = new List<string>();
//Split on each dash
foreach (string s in results)
final.AddRange(s.Split(‘-‘));
//Remove duplicates
final = final.Distinct().ToList();
//Return the first and the last element
return string.Format(“{0} – {1}”, final.First(), final.Last());
}

 

Hi,

 

The below code is for replace the specified attribute from the html.

protected override void Render(HtmlTextWriter writer)
{
using (System.IO.MemoryStream msOur = new System.IO.MemoryStream())
{
using (System.IO.StreamWriter swOur = new System.IO.StreamWriter(msOur))
{
HtmlTextWriter ourWriter = new HtmlTextWriter(swOur);
base.Render(ourWriter);
ourWriter.Flush();
msOur.Position = 0;
using (System.IO.StreamReader oReader = new System.IO.StreamReader(msOur))
{

string sTxt = oReader.ReadToEnd();
sTxt = sTxt.Replace(“rules=\”all\””, ” “);
Response.Write(sTxt);
oReader.Close();
}
}
}
}

Hi,

If, you want to remove the textbox auto-complete history from the browser,

then You have to set two properties for text box in aspx File.

Autocomplete=”Off” and AutoCompleteType=”Disabled”

This will dont prompt any previous values of textbox which you entered in the past.

From,

Vinit.

Hi,

If I want to select only limited(2) items from the listbox then using Jquery use the below code,

<head id=”Head1″ >
<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js”></script&gt;

</head>
<body>
<form runat=”server”>
<asp:ListBox ID=”ListBox1″ runat=”server” SelectionMode=”Multiple”>
<asp:ListItem Text=”Item1″></asp:ListItem>
<asp:ListItem Text=”Item2″></asp:ListItem>
<asp:ListItem Text=”Item3″></asp:ListItem>
<asp:ListItem Text=”Item4″></asp:ListItem>
</asp:ListBox>
</form>
</body>
<script type=”text/javascript”>
$(‘#<%= ListBox1.ClientID %>’).change(function()
{
if ($(this).find(‘option:selected’).length > 2)
{
this.options[this.selectedIndex].selected = false;
}
;
});
</script>

</html>

Using Javascript

<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head id=”Head1″ >

<script type=”text/javascript”>
function test()
{
var selectedCount = 0;
var listBox = document.getElementById(‘<%= ListBox1.ClientID %>’);
var i = 0;
do
{
if (listBox.options[i].selected)
{
selectedCount++;
if (selectedCount > 2)
{
listBox.options[i].selected = false;
break;
}
}
i++;

} while (i < listBox.options.length)
}
</script>

</head>
<body>
<form runat=”server”>
<asp:ListBox ID=”ListBox1″ runat=”server” onchange=”test()” SelectionMode=”Multiple”>
<asp:ListItem Text=”Item1″></asp:ListItem>
<asp:ListItem Text=”Item2″></asp:ListItem>
<asp:ListItem Text=”Item3″></asp:ListItem>
<asp:ListItem Text=”Item4″></asp:ListItem>
</asp:ListBox>
</form>
</body>
</html>

In The News.aspx Page

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”News.aspx.cs” Inherits=”News” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:GridView ID=”gvNews” runat=”server” AutoGenerateColumns=”false” ShowHeader=”false”>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID=”mylink” runat=”server” Text='<%# Eval(“Title”) %>’ NavigateUrl='<%# “~/NewsDetails.aspx?nid=” + DataBinder.Eval(Container, “RowIndex”) %>’></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

Codebehind in News.aspx page,

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;

public partial class News : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Gets the xml from the Url using XmlTextReader
XmlTextReader reader = new XmlTextReader(“http://www.rediff.com/rss/newsrss.xml&#8221;);
// creates a new instance of DataSet
DataSet ds = new DataSet();
// Reads the xml into the dataset
ds.ReadXml(reader);
// Assigns the data table to the datagrid
gvNews.DataSource = ds.Tables[3];
// Binds the datagrid
gvNews.DataBind();
}
}
}

In NewsDetails.aspx Page,

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”NewsDetails.aspx.cs” Inherits=”NewsDetails” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<iframe id=”myiframe” runat=”server” width=”100%” height=”600px”></iframe>
</div>
</form>
</body>
</html>

In Codebehind of NewsDetails.aspx page,

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;

public partial class NewsDetails : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString[“nid”] != null)
{
// Gets the xml from the Url using XmlTextReader
XmlTextReader reader = new XmlTextReader(“http://www.rediff.com/rss/newsrss.xml&#8221;);
// creates a new instance of DataSet
DataSet ds = new DataSet();
// Reads the xml into the dataset
ds.ReadXml(reader);

Int32 index = 0;
Int32.TryParse(Request.QueryString[“nid”].ToString(), out index);
string link = ds.Tables[3].Rows[index][“link”].ToString();

myiframe.Attributes.Add(“src”, link);
}
}
}

Declare a public string in the partial class of the CS file. like

public string str;

now place the str in the aspx file where you need to create the multiple textboxes like this

<%=str%>

now in the button click event using stringbuilder append the code that is used to create the TextBoxes dynamically like this

StringBuilder sb=new  StringBuilder();

sb.Append(“<asp:TextBox id=”xxx” runat=”server”/>”);

like this u can bind number of TextBoxes.

now assign this “sb” to the “str”

str=sb.ToString();

your Textboxes get created in the aspx file.


Calendar

May 2017
M T W T F S S
« Jan    
1234567
891011121314
15161718192021
22232425262728
293031  

Stats Of Blog

  • 6,792 hits

Cluster Map