ali raza

C#, .Net ,Asp.net and my thoughts

Monthly Archives: January 2007

How to use Check boxes in DataList

 This is html code for  datalist

 <asp:DataList ID=”dlist” runat=”server” RepeatColumns=”3″ OnItemDataBound=”dlist_ItemDataBound”                                      Width=”681px” ItemStyle-HorizontalAlign=”Left”>

<ItemTemplate>       

  <asp:CheckBox ID=”chkBlogSites” runat=”server” />

</ItemTemplate>

</asp:DataList>

   

IN Code Behind OnItemDatabound Event look like

 protected void dlist_ItemDataBound(object sender, DataListItemEventArgs e){

if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)

{

// Here we get the reference of check box

   CheckBox lit = (CheckBox)e.Item.FindControl(“chkBlogSites”);

   if (lit != null)

{

//bind it with database field, in my case i bind name feild of table

      lit.Text = DataBinder.Eval(e.Item.DataItem, “Name”).ToString() ;      lit.Checked = true;

         }

      }

    }

     Here the user define method where i get the value of selected textboxes 

 private void GetCheckedBlogs(){//DataListItem _obj = null;//naviagate in datalist item to fine which item is checked foreach (DataListItem _obj in dlist.Items)

     {

//check other than first row where names are mentioned

if (_obj.ItemIndex > -1)

{ 

   CheckBox _checkBox = (CheckBox)_obj.FindControl(“chkBlogSites”);

   if (_checkBox.Checked == true) 

     {

        Response.write(_checkBox.Text);

      }

   }

}

}

How to fetch data form Rss

This is some code I used to fetch data from rss links  and insert to database.  Where _RssAgent is containing business logic, having getters, setters and function to insertion in database. you can implement as your requirement 

XmlDocument doc = new XmlDocument();
doc.Load(xmlPath);
//Get all Items in the XML file
//Get reference to the first author node in the XML file
XmlNodeList titleList = doc.GetElementsByTagName(“item”);
// loop for all get
for (int _Count = 0; _Count <= titleList.Count – 1; _Count++)
{
XmlNode authorNode = doc.GetElementsByTagName(“item”)[_Count];
_RssAgent = new clsBlogPost();
foreach (XmlNode child in authorNode.ChildNodes)
{
if ((child.Name == “title”) && (child.NodeType == XmlNodeType.Element))
{

// Assign Title of post to some variable
_RssAgent.PostTitle = child.FirstChild.Value;
}
if ((child.Name == “description”) && (child.NodeType == XmlNodeType.Element))
{

// assign the description of rss item to variable
_RssAgent.Content = child.FirstChild.Value;
}
if ((child.Name == “link”) && (child.NodeType == XmlNodeType.Element))
{
_RssAgent.PostUrl = child.FirstChild.Value;
}
if ((child.Name == “pubDate”) && (child.NodeType == XmlNodeType.Element))
{

//if pub date found then assign it to date
_RssAgent.PostedDate = Convert.ToDateTime(child.FirstChild.Value);
}
}
if (_RssAgent.PostedDate == Convert.ToDateTime(“1/1/0001”))
{
_RssAgent.PostedDate = Convert.ToDateTime(“1/1/1900”);
}
_RssAgent.DateIndexed = DateTime.Now;
_RssAgent.BlogId = _RssId;

///insertion method call you can use ur own code here
_RssAgent.Insert(_RssAgent);
}

Url regular expression in asp.net

buddy here is regular expression for Url Vaildation

http://(%5B\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?”

asp.net control would be look like

<asp:RegularExpressionValidator ID=”RegularExpressionValidator1″ ControlToValidate=”XYZl” ValidationExpression=”http://(%5B\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?” runat=”server” ErrorMessage=”Invalid URL”></asp:RegularExpressionValidator>

enjoy

Url Vaildator in asp.net

buddy here is regular expression for Url Vaildation

http://(%5B\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?”

asp.net control would be look like

<asp:RegularExpressionValidator ID=”RegularExpressionValidator1″ ControlToValidate=”XYZl” ValidationExpression=”http://(%5B\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?” runat=”server” ErrorMessage=”Invalid URL”></asp:RegularExpressionValidator>

enjoy