Category Archives: ASP.NET MVC

Post Data using ASP.NET Web Browser Control

Hello People,

Its almost 2 AM here at my place, need a sound sleep from so many days but before that let me share this trick with you all.

Have you ever used ASP.NET Web Browser for your application, give it a try! its very useful. It really helped me sort out a big problem in one of my application where I previously was using HttpWebRequest but I got stuck when the page I was posting data to used a cookie checking before processing any request and the cookie was created using javascript.

As far as from my research I have found you cannot fire a javascript code on a page using HttpWebRequest, please correct me if I am wrong anywhere, anyways so this was my problem. Finally i came through Web Browser control and gave it a try. Because it works similar to any browser, client side script (javascript) aslo worked fine in it 🙂

Here is a small excerpt from my code

AxWebBrowser wb = new AxWebBrowser ();
object vPost = null;
object vHeaders = null;
object obj = null;
object url = “http://www.yoururl.com/yourpage”;
object cPostData = null;

cPostData = “var1=data1&var2=datra2&var3=data3&var4=data4”;

vHeaders = “Content-Type: application/x-www-form-urlencoded” + Convert.ToChar(10) + Convert.ToChar(13);
(You can add other headers as required)

vPost = ASCIIEncoding.ASCII.GetBytes((string)cPostData);
wb.Navigate2(ref url, ref obj, ref obj, ref vPost, ref vHeaders);

This is all it takes to post data programmatically to a page using Web Browser control in you desktop application.

Hope this helps you, if you have any queries please feel free to ask.

Populate Dropdown Using Enum (Asp.Net MVC)

Hi folks, I am new to Asp.Net MVC. I was trying to populate a dropdown from an Enum.

After searching on web couldn’t realy find a direct solution, so thought to do in my own way and finally  got this solution.

Now here is an excertp from my code.

MyEmun is the Enum from which I want to populate the dropdown.

public enum MyEnum

{

Day = 1 ,

Month = 2,

Year = 3

}

MyEnumList() is a function that return a collection of SelectListItem that is used to populate the Dropdown.

private IList<SelectListItem> MyEnumList()

{

string[] names = Enum.GetNames(typeof(MyEnum));

Array values = Enum.GetValues(typeof(MyEnum));

IList<SelectListItem> items = new List<SelectListItem>();

for (int i = 0; i < names.Length; i++)

{

int val = (int)values.GetValue(i);

items.Add(new SelectListItem

{

Text = names[i],

Value = val.ToString()

});

}

return items;

}

Pass the return value of MyEnumList()  using ViewData from your controller to the view where you have the dropdown

ViewData[“MyList”] = MyEnumList ();

And finally populate your dropdown

<%=Html.DropDownList(“MyDropDown”,new SelectList((IList)ViewData[“MyList “],”Value”,”Text”))%>

hope this would helps 🙂