Select and show selected row to label - c#

So, when I click Select I want to show in a Label all datas that contain one row. I have managed to make this, except DropdownList. When I click "Select" it's just empty.
protected void GridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
Label1.Text = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
Label2.Text = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;
Label3.Text = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;
}
P.S.: I have not done this programmatically. The only code I've wrote on .aspx.cs file is the code above.

Use this to find the value. its not showing the value because of template field control.
I have used the gridview control that you pasted in pastebin.
protected void GridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) {
Label drpValue =
(Label)this.GridView1.Rows[e.NewSelectedIndex].Cells[1].FindControl("Label1");
Lbl1.Text = drpValue.Text;
Lbl2.Text = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;
Lbl3.Text = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;
}

Related

how to change drop down list based on gridview selection

I have a drop down list which will load up with a series of customer ID numbers. I then have a gridview control on my page with the select hyperlink. When i click on the link to select the row in gridview i would like my dropdown to change to that number.
Below is what i have tried but doesn't work:
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (!GridView1.SelectedIndex.Equals(-1))
{
DropDownList ddl;
ddl = (DropDownList)form1.FindControl("ddl_Customers");
ddl.SelectedValue = (String)GridView1.SelectedDataKey.Values[0];
}
}
Handle the SelectedIndexChanged event for GridView1
void GridView1_SelectedIndexChanged(Object sender, EventArgs e) {
ddl_Customers.SelectedValue = GridView1.SelectedDataKey.Value.ToString();
}
You can directly use GridView1.SelectedValue.ToString() for this. To use that, you should define a datakeyname like this: <asp:Gridview DataKeyNames="CustomerID">
Then all you need is this:
void GridView1_SelectedIndexChanged(Object sender, EventArgs e) {
ddl_Customers.SelectedValue = GridView1.SelectedValue.ToString();
}

Retrieve the values from textbox which is dynamically created using C#

I have created few textbox dynamically while coding in the flow I have provided unique id for each and I have hard coded some values to all the text boxes using C#.
Now on click of button am trying to retrieve the values from the textbox for which I have used the below code, but its throwing an exception as OBJECT REFERENCE NOT SET TO INSTANCE OF AN OBJECT.
Please look at the below code, I have tried both the things but still am not getting. Please help me out.
Thanks
protected void btnPltGrap_onclick(object sender, EventArgs e)
{
//spny is my stack panel and txtX0 is my of the text box id
//Below is the 1st Try
TextBox tb = new TextBox();
tb= (TextBox)Master.FindControl("spnY").FindControl("txtX0");
string strx = tb.Text;
//Below is the 2nd Try
string strx = (spnY.FindControl("txtX0") as TextBox).Text;
}
Thanks
Am trying to use view state as per you told that i shlould recreate the controls ones again but am getting exception as Invalid Arguments. please go have a look.
protected void btnSet_onClick(object sender, EventArgs e)
{
Table tblMainY = new Table();
TableRow tblRow = new TableRow();
tblMainY.Controls.Add(tblRow);
TableCell tblCel = new TableCell();
TextBox txtdyn = new TextBox();
txtdyn.Text = "1";
txtdyn.ID = "txtY01";
txtdyn.Width = 50;
tblCel.Controls.Add(txtdyn);
tblRow.Controls.Add(tblCel);
splY.Controls.Add(tblMainY);
ViewState["temptbl"] = tblMainY
}
protected void btnPltGrap_onclick(object sender, EventArgs e)
{
splY.Controls.Add(ViewState["Temptbl"]);
}
Please help me out
I've had the same problem in the past.
What I did was give the dynamically-added control an ID, and made sure it retained that ID also on postback.
Once the postbacked control has the same ID as as before, Microsoft did magic and refilled the controls with the pre-postback values.
Read out this code once
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
this.NumberOfControls = 0; //very first time when page is loaded, value will be 0
else
this.createControls(); //if it is postback it will recreate the controls according to number of control has been created
}
//this is the base of this, it will hold the number of controls has been created, called properties
protected int NumberOfControls
{
get { return (int)ViewState["NumControls"]; }
set { ViewState["NumControls"] = value; }
}
//it will create the controls
protected void createControls()
{
int count = this.NumberOfControls;
for (int i = 0; i < count; i++) //loop for the total number of control.
{
TextBox tx = new TextBox(); //creating new control
tx.ID = "ControlID_" + i.ToString(); //in your solution you are giving static id, don't do that, assign id number dynamically, it will help you further, if you want to manipulate the controls for some other use
//Add the Controls to the container of your choice
form1.Controls.Add(tx);
}
}
//add new control
protected void addSomeControl()
{
TextBox tx = new TextBox();
tx.ID = "ControlID_" + NumberOfControls.ToString();
form1.Controls.Add(tx);
this.NumberOfControls++; //increment the number of control
}
protected void AddBtn_Click(object sender, EventArgs e)
{
addSomeControl();
}
Default.aspx
take placeholder tag in aspx file
< asp:PlaceHolder ID="PlaceHolder1" runat="server">
Default.aspx.cs
// adding/creating dynamic text box
TextBox txt = new TextBox();
txt.ID = "New_txt";
txt.TextMode = TextBoxMode.MultiLine;
txt.Text = dt.Rows[0]["message"].ToString();
txt.Width = 802;
txt.Height = 450;
txt.ReadOnly = true;
PlaceHolder1.Controls.Add(txt);
Retrive value from text box
string str = txt.Text;
some sample code in bellow link as my blog
Its explain for how to put and get textboxe's with values and validations in dynamicaly using panel control .
Let's go this url . and you can get good solutions
get and Create dynamic Textbox and dropdownlist with Validation
simple line for get textbox values in
TextBox objTextBox = (TextBox)PlaceHolder.FindControl("CorrecttextBoxName");
string value=objTextBox .Text;
You must recreate your controls on init to get it's value.
Here are some links
Get text from dynamically created textbox in asp.net
Edit 1
TextBox tb=(TextBox)ViewState["Temptbl"];
splY.Controls.Add(tb);

How to display a RepositoryItemHyperLinkEdit control inside a DevExpress GridControl

I have a DevExpress GridControl in my current WinForms application. I need to display a hyperlink control (RepositoryItemHyperLinkEdit) in a column. I have added the RepositoryItemHyperLinkEdit via designer, but when I am running the application, hyperlink is not displaying.
Like to display buttons we are using:
repositoryItemButtonEdit1.Buttons[0].Kind = DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph;
repositoryItemButtonEdit1.Buttons[0].Caption = "Get Sql Query";
So please tell me what I will write to display hyperlink in a column.
You can use the following code to display hyperlink in grid column:
GridColumn hyperLinkColumn = gridView1.Columns["Hyperlink"];
//...
RepositoryItemHyperLinkEdit hyperLinkEdit = new RepositoryItemHyperLinkEdit();
hyperLinkColumn.ColumnEdit = hyperLinkEdit; // this line associated hyperlink with column
hyperLinkEdit.OpenLink += hyperLinkEdit_OpenLink;
//...
void hyperLinkEdit_OpenLink(object sender, OpenLinkEventArgs e) {
MessageBox.Show("HyperLinkEdit clicked!");
}
If you want to display aditional button in the same column you can use the following approach:
hyperLinkEdit.Buttons[0].Kind = ButtonPredefines.Glyph;
hyperLinkEdit.Buttons[0].Caption = "Get SQL Query";
hyperLinkEdit.ButtonClick += hyperLinkEdit_ButtonClick;
hyperLinkColumn.ShowButtonMode = ShowButtonModeEnum.ShowAlways; // always display button in this column
//...
void hyperLinkEdit_ButtonClick(object sender, ButtonPressedEventArgs e) {
MessageBox.Show("HyperLinkEdit's button clicked!");
}
You didn't mentioned that you've set up the ColumnEdit property of the column to the repository item. If you haven't:
Note that you may have to use the gridView_MouseUp event in order to catch the click event without waiting that the grid give the focus to the cell.
gridColumn.ColumnEdit = new RepositoryItemHyperLinkEdit();
gridColumn.OptionsColumn.ReadOnly = true;
gridColumn.OptionsColumn.AllowEdit = false;
gridView.MouseUp += gridView_MouseUp;
private void gridViewDesk_MouseUp(object sender, MouseEventArgs e)
{
GridView gridView = (GridView) sender;
if (e.Button == MouseButtons.Left && e.Clicks == 1)
{
GridHitInfo hitInfo = gridView.CalcHitInfo(e.Location);
if (hitInfo.InRowCell && hitInfo.Column == this.gridColumn)
{
MessageBox.Show("Click " + hitInfo.RowHandle);
}
}
}

FormView Data Binding

I have for fields in my datasource that I want to combine and display in one label field. I have added a procedure to capture the databinding action but I don't how to get the data out of the datasource. I am displaying this information on a FormView is that makes anly difference. Can I get an example in c#?
For example -
protected void DisplayPayOut(object sender, EventArgs e)
{
Label Payout = FormView1.FindControl("PayoutLabel") as Label;
Payout.Text = datasource.field1 + datasource.field2;
}
I'm not fully sure but it seems like you're looking for something like the following:
protected void DisplayPayOut(object sender, EventArgs e)
{
Label Payout = FormView1.FindControl("PayoutLabel") as Label;
object dataItem = DataBinder.GetDataItem(FormView1);
Payout.Text = DataBinder.Eval(dataItem, "field1NameHere").ToString() + DataBinder.Eval(dataItem, "field2Namehere").ToString();
}

GridView selects wrong row for editing

I have a simple admin panel with a simple gridview that list all articles present in the database.
I've added a filtering doodle (a textbox + a button) that allows the user to filter the gridview by a article name.
The code for the filter:
protected void ButtonSearchArticle_Click(object sender, EventArgs e)
{
{
LinqDataSourceAdminArticles.Where = "Title.Contains(" + "\"" + TextBoxSearchArticle.Text + "\")";
LinqDataSourceAdminArticles.DataBind();
}
LinqDataSourceAdminArticles.DataBind();
}
The gridview has the default quick editing and deleting enabled on it. The problem is, after I filter it with that code, it starts to select wrong rows when I click the "edit" button. Any ideas how to solve this? I know it has something to do with a postback.
I've checked Why is My GridView FooterRow Referencing the Wrong Row? and Sorted gridview selects wrong row but those didn't solve my problem.
Thanks!
When you change the GridView's Select query in your button click, it only take effects for that request. because GridView's edit command causes a postback, and in the postback the Gridview works with visible index of the edited row but without the filtering.
the best thing to do is to remove your ButtonSearchArticle_Click code and put it into your Page_Load code like this
if (TextBoxSearchArticle.Text != ""){
LinqDataSourceAdminArticles.Where = "Title.Contains(" + "\"" + TextBoxSearchArticle.Text + "\")";
LinqDataSourceAdminArticles.DataBind();
LinqDataSourceAdminArticles.DataBind();}
//Your Page Load Event
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["s_event"] = "0"; // Initialize session variable
BindData(); // Gridview Binding
}
}
protected void BindData()
{
if ((Session["s_event"].ToString())=="1")
{
cmdstr_ = (Session["search_item"].ToString());
}
else
{
cmdstr_ = ""; // Your command string to populate gridview
}
// `enter code here`
}
protected void btnSearch_Click(object sender, EventArgs e)
{
Session["s_event"] = 1; // Search Event becomes 1.
// Your Search Logic here
Session["search_item"] = cmdstr;
// Bind Gridview here
}

Categories

Resources