Getting the available view for the sharepoint list and populating into the dropdownlist? - asp.net

1. Here the sample code i tried for getting the available views for the
sharepoint list and populating into the dropdownlist
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SPSite site = new SPSite(SPContext.Current.Site.Url);
foreach (SPWeb web in site.AllWebs)
{
SPListCollection spListCollection = web.GetListsOfType(SPBaseType.GenericList);
foreach (SPList splist in spListCollection)
{
ListNameList.Items.Add(splist.Title.ToString());
}
ListNameList.Items.Insert(0, "--Select--");
}
}
}
protected void ListNameList_SelectedIndexChanged(object sender,EventArgs e)
{
SPSite site = new SPSite(SPContext.Current.Site.Url);
SPWeb web = site.OpenWeb();
SPList selectedlist = web.Lists[ListNameList.SelectedValue];
foreach (SPView views in selectedlist.Views)
{
ViewNameList.Items.Add(views.Views.ToString());
}
}
I created the views in the sharepoint list,but in the above code
while looping i got the viewnames properly but when populating into
the dropdown list the output i am getting like Microsoft.Sharepoint
instaed of viewnames.
Can you help me out of this?

I think you just need to edit the line where you add in the new Item to the ViewNameList to use view.Title
protected void ListNameList_SelectedIndexChanged(object sender,EventArgs e)
{
SPSite site = new SPSite(SPContext.Current.Site.Url);
SPWeb web = site.OpenWeb();
SPList selectedlist = web.Lists[ListNameList.SelectedValue];
foreach (SPView views in selectedlist.Views)
{
ViewNameList.Items.Add(views.Title);
}
}
You also may want to order the views if the collection is not returning them in the order that you want.

Related

unable to persist data on postback in dotnetnuke7

I have my website running on dotnetnuke 7.4, i have a checklistbox which i bind on the page load, and after selecting items from it, user clicks on the submit button, the selected items should save in database, however when i click on the submit button, checklistbox gets blank, i tried to enable ViewState at :
Web.config level
Page Level
Control Level
But all in vain, it still unbinds checklistbox because of which everything disappears, i tried the same in plain .net and it works like a charm.
Is there any specific settings in dotnetnuke to support viewstate, or is there any other better option to achieve this.
Here's my code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Entities objEntities = new Entities();
List<Entities> obj = objEntities.GetList(2);
chkBox.DataSource = obj;
chkBox.DataTextField = "Name";
chkBox.DataValueField = "ID";
chkBox.DataBind();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
foreach (ListItem item in chkBox.Items)
Response.Write(item.Text + "<br />");
}
There's the issue. Remove that (!IsPostBack) check in your page_load event. Have your code to be like below. Else, only at first page load you are binding the datasource to control which gets lost in postback.
protected void Page_Load(object sender, EventArgs e)
{
Entities objEntities = new Entities();
List<Entities> obj = objEntities.GetList(2);
chkBox.DataSource = obj;
chkBox.DataTextField = "Name";
chkBox.DataValueField = "ID";
chkBox.DataBind();
}
OR, to be more efficient; refactor your code to a method like below and store the data object in Session variable like
private void GetDataSource()
{
List<Entities> obj = null;
if(Session["data"] != null)
{
obj = Session["data"] as List<Entities>;
}
else
{
Entities objEntities = new Entities();
obj = objEntities.GetList(2);
}
chkBox.DataSource = obj;
chkBox.DataTextField = "Name";
chkBox.DataValueField = "ID";
chkBox.DataBind();
Session["data"] = obj;
}
Call the method in your Page_Load event like
protected void Page_Load(object sender, EventArgs e)
{
GetDataSource();
}

ASP.Net Fill GridView with Entity Framework Code First

This is my page
Profiles pp;
Tasks t;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["User"] != null)
{
pp = (Profiles)Session["User"];
}
else
{
Response.Redirect("PageLogin.aspx");
}
}
private void data()
{
DBCOntext db= new DBCOntext ();
var manager= db.Tasks.Where(p => p.Personnel == pp.Username).Select(q => q.Manager).FirstOrDefault();
}
I took the data with data class. But I didn't fill gridview. How do I fill gridview? pls help.
Where are you data binding the gridview? You aren't using the manger variable anywhere that I can see.
GridView.DataSource = manager;
GridView.DataBind();

how to data bind to dropdownlist when checkbox is checked in asp.net

I have a project that using dropdownlist for choices.When check checkbox1 dropdown automatically bind data from database using table1 and when I check checkbox2 dropdown automatically binding data from database using table2.I do not want to use get data by using any button .How can I do that .Please help me.
here is code by using button:
public void LokasyonDoldur()
{
birimBUS = new BirimBUSV1();
List<BirimVO> birimVO = new List<BirimVO>();
DrpChcs.Items.Clear();
List<ListItem> items = new List<ListItem>();
birimVO = birimBUS.LokasyonlariGetir();
foreach (var item in birimVO)
{
items.Add(new ListItem(item.BirimAdi, item.ID.ToString()));
}
DrpChcs.Items.AddRange(items.ToArray());
}
public void BirimleriDoldur()
{
PoliklinikBUS poliklinikBUS = new PoliklinikBUS();
List<PoliklinikVO> poliklinikVO = new List<PoliklinikVO>();
DrpChcs.Items.Clear();
List<ListItem> items = new List<ListItem>();
poliklinikVO = poliklinikBUS.Poliklinikler();
foreach (var item in poliklinikVO)
{
items.Add(new ListItem(item.PoliklinikAdi, item.ID.ToString()));
}
DrpChcs.Items.AddRange(items.ToArray());
}
protected void BtnLokasyon_Click(object sender, EventArgs e)
{
if (ChckLctn.Checked == true && ChckBrm.Checked==false)
{
LokasyonDoldur();
}
else if (ChckLctn.Checked == false && ChckBrm.Checked == true)
{
BirimleriDoldur();
}
else
{
}
Button1.Visible = true;
BtnLokasyon.Visible = false;
}
protected void DrpChcs_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
KirilimId = Int32.Parse(DrpChcs.SelectedValue);
BPolikilinikID= KirilimId;
}
but I do not want to use this one.
ohh its another language. its hard to read. but what you basicly have to do is check which checkbox is checked in the page load and then load the dropdown based on what is loaded.
something like this. (I have typed it from my head so its not like copy-paste but you get the idea)
page_load
{
if(checkbox1.checked)
{
dropdown.dataitems = items1;
dropdown.databind();
return;
}
if(checkbox2.checked)
{
dropdown.dataitems = items2;
dropdown.databind();
return;
}
}
YOu can call the Button1_click event from the Dropdown list selected index changed event like this
Button1_Click(Button1,new EventArgs());
and in this you can hide that button from the page and in code behind you are calling the same function
OR
You can refactor the code in a seperate function from the button click event and call that function in the selected index changed event.
Please let me knwo if I misunderstood your question
Thanks

Using List<T> causes second Page Load in ASP.Net

I have a fairly simple web application that gets a list of items from a database (in a DataTable), and binds a view of that DataTable to a Repeater.
When converting my DataTable to a List (which is done in a class library), Page Load fires a second time! Walking though the debugger, the same items are in the list that were in the DataTable.
The only code in my page was:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
rptOffers.DataSource = DataAccess.GetOfferList(offerId); // returns List<T>
rptOffers.DataBind();
}
}
public static List<OfferItem> GetOfferList(int offerId)
{
DataTable dtOffers = GetOfferData(offerId);
List<OfferItem> offers = new List<OfferItem>();
// loop throw all of the offers
foreach (DataRow dr in dtOffers.Rows)
{
// add each offer to the List<>
OfferItem currentOffer = new OfferItem();
// initialize the OfferItem properties...
offers.Add(currentOffer);
}
return offers;
}
When I change it back to this, it works fine:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
rptOffers.DataSource = DataAccess.GetOfferItems(offerId);
rptOffers.DataBind();
}
}
Is there anything else I need to do in my List to keep it from running Page Load again?
I have experienced this issue when AutoEventWireup is set to true in my .aspx file
<%# ... AutoEventWireup="True" ... %>
and my InitializeComponent function still contains a hookup for Page_Load
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}

Read data from SqlDataSource or GridView

I have SqlDataSource and GridView on web form. GridView.DataSourceID = mySqlDataSource.
When I call myGridView.DataBind(), all data successfully bind on the page.
How is it possible to read already got data from mySqlDataSource or myGridView objects as DataTable or DataView? Thanks
The data in the gridview can read by using FindControl property of Gridview control. For example, for reading values set in the Checkbox column in the grid.
for (i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("checkbox1");
//here code for using value captured in chk
}
Provided your data set isn't gigantic, you could store it in the Session, bind your GridView to the Session data, and then re-use the Session data in your other web objects.
Your code would then look something like this (you'll have to forgive any minor inaccuracies -- I'm away from my development box at the moment):
protected override OnInit(object sender, EventArgs e)
{
base.OnInit();
if (Page.IsPostback == false)
{
SetSessionData();
}
//
// Set your GridView, DataTable, DataView, etc. events here.
//
}
void SetSessionData();
{
List<YourDataBoundObject> myDataBoundObject = GetYourDataBoundObject(); // Or Collection<T>, IEnumerable<T>, etc.
Session["data"] = myDataBoundObject;
}
void YourGridView_Load(object sender, EventArgs e)
{
BindYourGridView();
}
void BindYourGridView()
{
YourGridView.DataSource = GetSessionData();
YourGridView.DataBind();
}
List<YourDataBoundObject> GetSessionData()
{
return (List<YourDataBoundObject>) Session["data"];
}
void YourDataTable_Load(object sender, EventArgs e)
{
BindYourDataTable();
}
void BindYourDataTable()
{
YourDataTable.DataSource = GetSessionData();
YourDataTable.DataBind();
}

Resources