A basic task but unable to achieve.
The data template is like this:
Comment --(template ID is {AB86861A-6030-46C5-B394-E8F99E8B87DB})
-Comment --section
-Author --field
-Comment Text --field
I am creating a new item of that template as follows
protected void btnSubmit_Click(object sender, EventArgs e)
{
Sitecore.Data.Database masterDb = Sitecore.Configuration.Factory.GetDatabase("master");
Item parentItem = Sitecore.Context.Item;
var template = masterDb.GetTemplate("{AB86861A-6030-46C5-B394-E8F99E8B87DB}");
using (new Sitecore.SecurityModel.SecurityDisabler())
{
Item newItem = parentItem.Add(Sitecore.DateUtil.IsoNow, template);
try
{
if (newItem != null)
{
newItem.Editing.BeginEdit();
newItem["Author"] = txtAuthor.Text;
newItem["Comment Text"] = txtContent.Text;
newItem.Editing.EndEdit();
}
}
catch
{
//...log error
newItem.Editing.CancelEdit();
}
}
txtAuthor.Text = string.Empty;
txtContent.Text = string.Empty;
}
The item gets saved with the timestamp as name, as I see in the content tree. Now when I try to see the values (Author, comment text), they are empty. So, I doubt , if they were saved in the first place.
Code to see comments, using another Usercontrol.
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Database masterDb = Factory.GetDatabase("master");
Item parentItem = Sitecore.Context.Item;
var commentTemplate = masterDb.GetTemplate("{AB86861A-6030-46C5-B394-E8F99E8B87DB}");
var commentsList = parentItem.Children.Where(x => x.TemplateID == commentTemplate.ID);
Item item = commentsList.FirstOrDefault();
Response.Write(item["Author"] + "$$" + item["Comment Text"]);
}
}
The result is only
$$
What am i missing here.
UPDATE:
The mistake was with the ID being referenced. I was using the Template ID of the 'Comment' data template. It has to be its ID.
Related
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();
}
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
I've a devexpress XtraGrid Control. But, I couldn't get the ID of a by default selected row when the winform loads. I know how to get it when the user clicks on the grid.
Here is the code snapshot:
private void Form1_Load(object sender, EventArgs e)
{
grid1.DataSource = bindData(DataClassesDataContext.Table1.ToList());
ID = Convert.ToInt32(gridView.GetRowCellValue(gridView.FocusedRowHandle, "ID"));
XtraMessageBox.Show(ID.ToString());
}
public BindingSource bindData(object obj)
{
BindingSource ctBinding;
try
{
ctBinding = new BindingSource();
ctBinding.DataSource = obj;
return ctBinding;
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
If I understand you correctly, you need something like this:
private void Form1_Shown(object sender, EventArgs e)
{
grid1.DataSource = bindData(DataClassesDataContext.Table1.ToList());
var item = gridView.GetFocusedRow() as YourDataType
if(item != null)
{
ID = item.ID;
XtraMessageBox.Show(ID.ToString());
}
}
assuming what your bindData returns a typed collection of some kind.
** Update **
Moving the code to form_Shown seemed to do the trick.
When my page loads, it queries the database for some values and populates some text boxes with them:
protected void Page_Load(object sender, EventArgs e)
{
tadbDataContext tadb = new tadbDataContext();
Dictionary<string, string> hexColors = tadb.msp_silentAuctionColors.ToDictionary(t => t.colorDescription, t => t.colorValue);
tbTextColor.Text = hexColors["textColor"];
tbAltColor.Text = hexColors["altColor"];
tbBackgroundColor.Text = hexColors["backgroundColor"];
}
I then change the value and try to resubmit to the database, by clicking a button which does the following:
using (tadbDataContext tadb = new tadbDataContext())
{
var textColor = tadb.msp_silentAuctionColors.Single(x => x.colorDescription == "textColor");
var altColor = tadb.msp_silentAuctionColors.Single(x => x.colorDescription == "altColor");
var backgroundColor = tadb.msp_silentAuctionColors.Single(x => x.colorDescription == "backgroundColor");
textColor.colorValue = tbTextColor.Text;
altColor.colorValue = tbAltColor.Text;
backgroundColor.colorValue = tbBackgroundColor.Text;
tadb.SubmitChanges();
}
The value that is posted back is the original (not the changed) value. If I comment out the lines that populate the text boxes on load it works fine.
That is because you didn't wrap the databinding stuff in a IsPostBack-check in Page_Load. Hence you're always overwriting the changed value with the old from database.
So you simply have to do this:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
tadbDataContext tadb = new tadbDataContext();
Dictionary<string, string> hexColors = tadb.msp_silentAuctionColors.ToDictionary(t => t.colorDescription, t => t.colorValue);
tbTextColor.Text = hexColors["textColor"];
tbAltColor.Text = hexColors["altColor"];
tbBackgroundColor.Text = hexColors["backgroundColor"];
}
}
I just display my uploaded file details in my GridView, so there will be only one row in my GridView - multiple files are not allowed.
When I delete that single row and try to upload a new file, it is showing 2 rows (the new file and the deleted file).
I already tried using GridView.DataSource = null and GridView.DataBind().
Note: I've rebinded my GridView after the delete, but it still shows the deleted file.
protected void DeleteLinkButton_Click(object sender, EventArgs e)
{
if (Session["name"] != null)
{
string strPath = Session["filepath"].ToString();
System.IO.File.Delete(strPath);
GridView2.Rows[0].Visible = false;
Label8.Text = "";
Session["filename"] = null;
Button3.Enabled = true;
}
GridView2.DataBind();
}
In some cases I've had to do something like this:
//page level variable
bool refreshRequired = false;
protected void DeleteLinkButton_Click(object sender, EventArgs e)
{
if (Session["name"] != null)
{
string strPath = Session["filepath"].ToString();
System.IO.File.Delete(strPath);
GridView2.Rows[0].Visible = false;
Label8.Text = "";
Session["filename"] = null;
Button3.Enabled = true;
refreshRequired = true;
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
if(refreshRequired)
{
//whatever you to to set your grids dataset, do it here
//but be sure to get the NEW data
}
}
At the time of Delete, your grid is bound to the old data. When you change the data, you must rebind it to the new.