protected void RadButton1_Click(object sender, EventArgs e)
{
RadTreeList1.ItemCommand -= new EventHandler<TreeListCommandEventArgs>(RadTreeList1_ItemCommand);
RadButton radbutton1 = (RadButton)Form.FindControl("RadButton1");
TreeListDataItem dataItem = e.Item as TreeListDataItem;
Hashtable table = new Hashtable();
table["RowId"] = (dataItem.FindControl("Label1") as Label).Text;
table["Alias"] = (dataItem.FindControl("Label2") as Label).Text;
}
There I am getting error at Item as i dont have TreeListCommandEventArgs in radbutton pls help me in this
If I replace EventArgs with TreeListCommandEventArgs that doesnt meaningul and becomes error at runtime.....
protected void RadButton1_Click(object sender, EventArgs e)
{
RadTreeList1.ItemCommand -= new EventHandler<TreeListCommandEventArgs>(RadTreeList1_ItemCommand);
ContentPlaceHolder contentPage = this.Page.Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
RadButton R = sender as RadButton;
RadButton radbutton1 = R.Parent.FindControl("RadButton1") as RadButton;
CommandEventArgs e2 = new CommandEventArgs(null, radbutton1.CommandArgument);
TreeListCommandEventArgs e1 = new TreeListCommandEventArgs(null, radbutton1.CommandArgument, e2);
TreeListDataItem dataItem = e1.Item as TreeListDataItem;
Hashtable table = new Hashtable();
table["RowId"] = (dataItem.FindControl("Label1") as Label).Text;
table["Alias"] = (dataItem.FindControl("Label2") as Label).Text;
}
protected void RadButton1_Click(object sender, EventArgs e)
{
ContentPlaceHolder contentPage = Page.Master.FindControl("ContentPlaceHolder2") as ContentPlaceHolder;
RadButton radbutton1 = (RadButton)contentPage.FindControl("RadButton1");
object mysender = (object)radbutton1;
CommandEventArgs e2 = new CommandEventArgs(null, radbutton1.CommandArgument);
RadButton1_Click(mysender, e2);
TreeListCommandEventArgs e1 = new TreeListCommandEventArgs(null,radbutton1.CommandArgument,e2);
TreeListDataItem dataItem = e1.Item as TreeListDataItem;
Hashtable table = new Hashtable();
table["RowId"] = (dataItem.FindControl("Label1") as Label).Text;
table["Alias"] = (dataItem.FindControl("Label2") as Label).Text;
}
Related
I have the following class which is the class for creating a gridview dynamic:
public class DynamicGridViewImageButtonTemplate : ITemplate
{
string _ColName;
DataControlRowType _rowType;
int _Count;
int _Kind;
public DynamicGridViewImageButtonTemplate(string ColName, DataControlRowType RowType, int Kind)
{
_ColName = ColName;
_rowType = RowType;
_Kind = Kind;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (_rowType)
{
case DataControlRowType.DataRow:
if (_Kind == 0)
{
Label lbl = new Label();
lbl.DataBinding += new EventHandler(this.lbl_DataBind);
container.Controls.Add(lbl);
}
else
if (_Kind == 1)
{
ImageButton vImageButton = new ImageButton();
vImageButton.ID = "btnGetVoucher";
vImageButton.CommandArgument = string.Format("<%# Eval({0}) %>", "Voucher");
vImageButton.CssClass = "divButton";
vImageButton.Height = 25;
vImageButton.Width = 25;
vImageButton.Command += new CommandEventHandler(btnGetVoucher_Command);
vImageButton.DataBinding += new EventHandler(this.imgBtn_DataBind);
container.Controls.Add(vImageButton);
}
break;
default:
break;
}
}
and the following c# code for assigning methods runtime to the imagebutton which I have created in a column:
protected void btnGetVoucher_Command(object sender, CommandEventArgs e)
{
ImageButton btn = sender as ImageButton;
}
protected void gridview_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton vImageButton = (ImageButton)e.Row.FindControl("btnGetVoucher");
vImageButton.Command += new CommandEventHandler(btnGetVoucher_Command);
}
}
and the creating of the gridcolumn is as follows:
GridView vGridView = new GridView();
vGridView.RowCreated += new GridViewRowEventHandler(gridview_RowCreated);
vTemplateField = new TemplateField();
vTemplateField.HeaderTemplate = new DynamicGridViewImageButtonTemplate("Voucher", DataControlRowType.Header, 0);
vTemplateField.ItemTemplate = new DynamicGridViewImageButtonTemplate("Voucher", DataControlRowType.DataRow, 1);
vGridView.Columns.Add(vTemplateField);
And at last the databind here:
private void imgBtn_DataBind(Object sender, EventArgs e)
{
ImageButton btn = (ImageButton)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
btn.CommandArgument = DataBinder.Eval(row.DataItem, "Voucher").ToString();
}
But I can't step into the method btnGetVoucher_Command :-( I think that everything has been made correctly - but there must be something I have missed somewhere...
I can access the button in my Row_Created routine - but afterwards not use the GetVoucherCommand :-( the event is not fired :-(
So my question is why can't I step into that routine?
Thanks in advance,
Michael
Update:
The full class is here...
public class DynamicGridViewTextTemplate : ITemplate
{
string _ColNameText;
DataControlRowType _rowType;
public DynamicGridViewTextTemplate(string ColNameText, DataControlRowType RowType)
{
_ColNameText = ColNameText;
_rowType = RowType;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (_rowType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = "<b>" + _ColNameText + "</b>";
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
ImageButton vImageButton = new ImageButton();
vImageButton.ID = "btnGetVoucher";
vImageButton.ImageUrl = "~/Images/Icons/icons8-download-from-the-cloud-50.png";
vImageButton.PostBackUrl = "#DownloadVoucher";
vImageButton.CommandArgument = string.Format("<%# Eval({0}) %>", "Voucher");
vImageButton.ToolTip = "Se og download bilag";
vImageButton.CssClass = "divButton";
vImageButton.Height = 25;
vImageButton.Width = 25;
vImageButton.DataBinding += new EventHandler(this.imgBtn_DataBind);
vImageButton.CommandName = "DownloadVoucher";
vImageButton.Command += new CommandEventHandler(btnGetVoucher_Command);
container.Controls.Add(vImageButton);
break;
default:
break;
}
}
private void imgBtn_DataBind(Object sender, EventArgs e)
{
ImageButton btn = (ImageButton)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
btn.CommandArgument = DataBinder.Eval(row.DataItem, _ColNameText).ToString();
}
}
public class DynamicGridViewImageButtonTemplate : ITemplate
{
string _ColNameText;
DataControlRowType _rowType;
public DynamicGridViewImageButtonTemplate(string ColNameText, DataControlRowType RowType)
{
_ColNameText = ColNameText;
_rowType = RowType;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (_rowType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = "<b>" + _ColNameText + "</b>";
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
ImageButton vImageButton = new ImageButton();
vImageButton.ID = "btnGetVoucher";
vImageButton.ImageUrl = "~/Images/Icons/icons8-download-from-the-cloud-50.png";
vImageButton.PostBackUrl = "#DownloadVoucher";
vImageButton.CommandArgument = string.Format("<%# Eval({0}) %>", "Voucher");
vImageButton.ToolTip = "Se og download bilag";
vImageButton.CssClass = "divButton";
vImageButton.Height = 25;
vImageButton.Width = 25;
vImageButton.DataBinding += new EventHandler(this.imgBtn_DataBind);
vImageButton.CommandName = "DownloadVoucher";
vImageButton.Command += new CommandEventHandler(btnGetVoucher_Command);
container.Controls.Add(vImageButton);
break;
default:
break;
}
}
protected void btnGetVoucher_Command(object sender, CommandEventArgs e)
{
ImageButton btn = sender as ImageButton;
}
private void imgBtn_DataBind(Object sender, EventArgs e)
{
ImageButton btn = (ImageButton)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
btn.CommandArgument = DataBinder.Eval(row.DataItem, _ColNameText).ToString();
}
}
The events is here...
protected void gridview_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton vImageButton = (ImageButton)e.Row.FindControl("btnGetVoucher");
}
}
protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton vImageButton = (ImageButton)e.Row.FindControl("btnGetVoucher");
}
}
protected void gridview_RowCommand(object sender, CommandEventArgs e)
{
if (e.CommandName == "DownloadVoucher")
{
int index = Convert.ToInt32(e.CommandArgument);
GridView grid = sender as GridView;
GridViewRow row = grid.Rows[index];
ImageButton vImageButton = (ImageButton)row.FindControl("btnGetVoucher");
}
}
The creating of the gridview is here...
GridView vGridView = new GridView();
vGridView.AutoGenerateColumns = false;
vGridView.ShowHeaderWhenEmpty = true;
vGridView.ShowHeader = true;
vGridView.HeaderStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#C6E0B4");
vGridView.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
vGridView.RowCreated += new GridViewRowEventHandler(gridview_RowCreated);
vGridView.RowDataBound += new GridViewRowEventHandler(gridview_RowDataBound);
vGridView.RowCommand += new GridViewCommandEventHandler(gridview_RowCommand);
vTemplateField = new TemplateField();
vTemplateField.HeaderTemplate = new DynamicGridViewImageButtonTemplate("Voucher", DataControlRowType.Header);
vTemplateField.ItemTemplate = new DynamicGridViewImageButtonTemplate("Voucher", DataControlRowType.DataRow);
vGridView.Columns.Add(vTemplateField);
micheal.
in this code, i can't see event handling code in InstantiateIn method.
ex) like this.
lbl.DataBinding += new EventHandler(this.btnGetVoucher_Command);
DynamicGridViewTextTemplate class constructor args are 3 count.
but, when using DynamicGridViewTextTemplate constructor,
used only 2 count args.
where is kind?
try it.
vTemplateField.HeaderTemplate = new DynamicGridViewImageButtonTemplate("Voucher", DataControlRowType.Header, 0);
vTemplateField.ItemTemplate = new DynamicGridViewImageButtonTemplate("Voucher", DataControlRowType.DataRow, 1);
best example in msdn
https://learn.microsoft.com/ko-kr/dotnet/api/system.web.ui.webcontrols.templatefield.-ctor?view=netframework-4.8
I hope this answer helps you.
I'm trying to get Tweet ID via TweetInvi API but always get null point exception. I'm out of options and can't understand what i did wrong. Here is my code. Problem occurs when i select desired Item from view and press retweet.
namespace MIF_TwitterApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Auth.SetUserCredentials("KEY", "KEY", "KEY",
"KEY");
}
private void Form1_Load(object sender, EventArgs e)
{
var user = User.GetAuthenticatedUser();
//PROFILE
profileImage.ImageLocation = user.ProfileImageUrlFullSize;
nameLabel.Text = user.Name;
usernameLabel.Text = "#" + user.ScreenName;
followersLabel.Text = "Followers: " + user.FollowersCount;
}
//Tweeting with PICS
private void tweetBtn_Click(object sender, EventArgs e)
{
if (tweetBox.Text != "")
{
if (imgUploadPath.Text != "")
{
byte[] file = File.ReadAllBytes(imgPreview.ImageLocation);
Tweet.PublishTweetWithImage(tweetBox.Text, file);
imgPreview.ImageLocation = "";
imgUploadPath.Text = "";
tweetBox.Clear();
MessageBox.Show("Tweet posted!");
}
else
{
Tweet.PublishTweet(tweetBox.Text);
MessageBox.Show("Tweet posted!");
tweetBox.Clear();
}
}
else
{
MessageBox.Show("Please enter text!");
tweetBox.Clear();
}
}
private void addImg_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
imgPreview.ImageLocation = ofd.FileName;
imgUploadPath.Text = ofd.FileName;
}
private void dropImg_Click(object sender, EventArgs e)
{
imgPreview.ImageLocation = "";
imgUploadPath.Text = "";
}
private void timelineBtn_Click(object sender, EventArgs e)
{
var user = User.GetAuthenticatedUser();
var getTweets = Timeline.GetHomeTimeline(40);
listView1.Clear();
listView1.View = View.Details;
listView1.GridLines = true;
listView1.FullRowSelect = true;
listView1.Columns.Add("Tweet", 570);
listView1.Columns.Add("Created By", 130);
listView1.Columns.Add("Create Time", 130);
listView1.Columns.Add("Likes", 60);
listView1.Columns.Add("Retweets", 70);
int x = 0;
foreach (var t in getTweets)
{
ListViewItem listItem = new ListViewItem(t.Text);
listItem.SubItems.Add(t.CreatedBy.ScreenName.ToString());
listItem.SubItems.Add(t.CreatedAt.ToString());
listItem.SubItems.Add(t.FavoriteCount.ToString());
listItem.SubItems.Add(t.RetweetCount.ToString());
listView1.Items.Add(listItem);
listView1.Items[x].Tag = t.Id;
}
}
private void postsBtn_Click(object sender, EventArgs e)
{
var user = User.GetAuthenticatedUser();
var getTweets = Timeline.GetUserTimeline(user, 40);
listView1.Clear();
listView1.View = View.Details;
listView1.GridLines = true;
listView1.FullRowSelect = true;
listView1.Columns.Add("Tweet", 570);
listView1.Columns.Add("Created By", 130);
listView1.Columns.Add("Create Time", 130);
listView1.Columns.Add("Likes", 60);
listView1.Columns.Add("Retweets", 70);
int x = 0;
foreach (var t in getTweets)
{
ListViewItem listItem = new ListViewItem(t.Text);
listItem.SubItems.Add(t.CreatedBy.ScreenName.ToString());
listItem.SubItems.Add(t.CreatedAt.ToString());
listItem.SubItems.Add(t.FavoriteCount.ToString());
listItem.SubItems.Add(t.RetweetCount.ToString());
listView1.Items.Add(listItem);
listView1.Items[x].Tag = t.Id;
}
}
private void retweetBtn_Click(object sender, EventArgs e)
{
var checkedItems = listView1.SelectedItems;
long a = (long)listView1.SelectedItems[0].Tag;
var user = User.GetAuthenticatedUser();
var retweet = Tweet.PublishRetweet(a);
MessageBox.Show("Retweet was successfull");
listView1.Items.Clear();
}
}
}
Breakes on long a, I can't figure out how to get that Long Tweet ID
Here is the full code
Problem was that my X was not incrementing correctly as it was out of for loop.
int x = 0;
foreach (var t in getTweets)
{
ListViewItem listItem = new ListViewItem(t.Text);
listItem.SubItems.Add(t.CreatedBy.ScreenName.ToString());
listItem.SubItems.Add(t.CreatedAt.ToString());
listItem.SubItems.Add(t.FavoriteCount.ToString());
listItem.SubItems.Add(t.RetweetCount.ToString());
listView1.Items.Add(listItem);
listView1.Items[x].Tag = t.Id;
x = x +1;
}
I select one value on the ddl , and it does not show the products in the page. The selected value remains binded but the page is blank.
Also , if I just call function getCat() without using if(!ispostback). When i load the page the drop down list is stuck on the first value , but it shows the products in page.
Drop Down List:
<asp:dropdownlist runat="server" id="ddcateg" AutoPostBack="true" onselectedindexchanged="Ddcateg_SelectedIndexChanged"></asp:dropdownlist>
This is the implementation:
protected void Page_Load(object sender, EventArgs e)
{
//afisare();
if (!IsPostBack)
{
getCateg();
}
}
public void getCateg()
{
ProdusTipModel model = new ProdusTipModel();
FarmacieEntities db = new FarmacieEntities();
var lizt = (from c in db.ProdusTips select c).ToList();
ddcateg.DataSource = lizt;
ddcateg.DataValueField = "ID";
ddcateg.DataTextField = "Name";
ddcateg.DataBind();
ddcateg.SelectedIndexChanged += Ddcateg_SelectedIndexChanged;
}
public void afisare2(List<Produ> z)
{
ProdusModel mdl = new ProdusModel();
foreach (var produs in z)
{
Panel produsePnl = new Panel();
ImageButton imageButton = new ImageButton();
produsePnl.BorderColor = Color.AliceBlue;
Label lblNume = new Label();
Label lblPret = new Label();
produsePnl.BorderStyle = BorderStyle.Groove;
produsePnl.BorderColor = Color.LightSkyBlue;
imageButton.ImageUrl = "~/Img/Produse/" + produs.Image;
imageButton.CssClass = "imgProdus";
imageButton.PostBackUrl = "~/Pages/PaginaProdus.aspx?id=" + produs.ID;
lblNume.Text = produs.Name;
lblNume.CssClass = "numeProd";
lblPret.Text = produs.Price + "lei";
lblPret.CssClass = "produsPret";
produsePnl.Controls.Add(imageButton);
produsePnl.Controls.Add(new Literal { Text = "<br /" });
produsePnl.Controls.Add(lblNume);
produsePnl.Controls.Add(new Literal { Text = "<br /" });
produsePnl.Controls.Add(lblPret);
pnlProduse.Controls.Add(produsePnl);
}
}
private void Ddcateg_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList selectedList = (DropDownList)sender;
int selectedLit = Convert.ToInt32(selectedList.SelectedValue);
ProdusModel mdl = new ProdusModel();
List<Produ> list = mdl.GetProdCateg(selectedLit).ToList();
afisare2(list);
}
Your problem could be that your code behind method is private:
private void Ddcateg_SelectedIndexChanged(object sender, EventArgs e)
Try making it protected or public so it can be seen by the aspx page.
protected override void OnInit(EventArgs e)
{
ListBox lst = new ListBox();
lst.Attributes["class"] = "cat-list";
lst.DataSource = cat.list(Convert.ToInt32(0));
lst.DataTextField = "catName";
lst.DataValueField = "catId";
lst.DataBind();
lst.AutoPostBack = true;
lst.SelectedIndexChanged += Lst_SelectedIndexChanged;
Panel pnl = new Panel();
pnl.Attributes["class"] = "col-sm-2 col-xs-12";
pnl.Controls.Add(lst);
categories.Controls.Add(pnl);
}
private void Lst_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox listBox = (ListBox)sender;
ListBox lst = new ListBox();
lst.Attributes["class"] = "cat-list";
lst.DataSource = cat.list(Convert.ToInt32(listBox.SelectedValue));
lst.DataTextField = "catName";
lst.DataValueField = "catId";
lst.DataBind();
lst.AutoPostBack = true;
lst.SelectedIndexChanged += Lst_SelectedIndexChanged;
Panel pnl = new Panel();
pnl.Attributes["class"] = "col-sm-2 col-xs-12";
pnl.Controls.Add(lst);
categories.Controls.Add(pnl);
}
The first Listbox selectedindexchanged event working but second Listbox selectedindexchanged event not working. If I select a option on second listbox, second listbox lost.Just keep selected option on first listbox. What can I do about that? Please help me.
I hope that help you
Panel pnl = new Panel();
ListBox lst1 = new ListBox();
ListBox lst2 = new ListBox();
ListBox lst3 = new ListBox();
protected override void OnInit(EventArgs e)
{
lst1.Attributes["class"] = "cat-list";
lst1.AutoPostBack = true;
lst1.SelectedIndexChanged += Lst_SelectedIndexChanged;
lst2.Attributes["class"] = "cat-list";
lst2.AutoPostBack = true;
lst2.SelectedIndexChanged += Lst_SelectedIndexChanged;
lst2.Visible = false;
lst3.Attributes["class"] = "cat-list";
lst3.AutoPostBack = true;
lst3.SelectedIndexChanged += Lst3_SelectedIndexChanged;
lst3.Visible = false;
pnl.Attributes["class"] = "col-sm-2 col-xs-12";
pnl.Controls.Add(lst1);
pnl.Controls.Add(lst2);
pnl.Controls.Add(lst3);
categories.Controls.Add(pnl);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lst1.DataSource = cat.list(Convert.ToInt32(0));
lst1.DataTextField = "catName";
lst1.DataValueField = "catId";
lst1.DataBind();
}
}
private void Lst_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox listBox = (ListBox)sender;
ListBox lst;
if (listBox == lst1)
lst = lst2;
else
lst = lst3;
lst.DataSource = cat.list(Convert.ToInt32(listBox.SelectedValue));
lst.DataTextField = "catName";
lst.DataValueField = "catId";
lst.DataBind();
lst.Visible = true;
}
private void Lst3_SelectedIndexChanged(object sender, EventArgs e)
{
//your code for third Listbox
}
I have a listbox in C# and want it to refresh after I added a new item(which gets opened with a new form dialog)
Here is my code which doesn't work.
private void showAllItems()
{
itemList = Db.getAllItems();
lb_itemList.DataSource = itemList;
}
private void showItemPreview(object sender, EventArgs e)
{
string curItem = lb_itemList.SelectedItem.ToString();
briefPreviewList = Db.getItemBriefPreview(curItem);
string itemInfos = string.Join(",", briefPreviewList.ToArray());
string[] infos = itemInfos.Split(',');
l_itemDB.Text = curItem;
l_CategoryDB.Text = infos[0];
}
private void b_addItem_Click(object sender, EventArgs e)
{
int uid = 1;
AddItem addItemForm = new AddItem(uid);
addItemForm.ShowDialog();
CurrencyManager cm = (CurrencyManager)BindingContext[itemList];
cm.Refresh();
}
I assume when you insert a new item it gets stored into the database, if this is the case then all you need to do is reset the datasource:
private void b_addItem_Click(object sender, EventArgs e)
{
int uid = 1;
AddItem addItemForm = new AddItem(uid);
addItemForm.ShowDialog();
addItemForm.Dispose();
this.showAllItems();
}