Obtaining the ctl property from a gridview control in C#, ASP.Net - c#

I am trying to obtain the ctl property that is specific to each row in a gridview. In my scenario, there is a list of titles, each title is displayed as a hyperlink using the LinkButton. When this is clicked, I would like to pass the ctl property (or what ever value is specific to that title, to the database and pull the information that is relative to the value).
I guess my first step is to obtain the ctl value. Could someone please help me get on my way. Thanks in advance.

You will need to keep track of the control ID that is written to the page in the row created event. Make a integer to increment for each row written and save it as a command argument on a link in the grid.
linkbutton CommandArgument='<%# Eval("some_id") %>'
protected void linkButton_Click(object sender, EventArgs e)
{
LinkButton linkButton = (LinkButton) sender;
if (linkButton != null)
{
if (linkButton.CommandArgument != null)
{
...some code...
}
}
}

Related

Textbox control from gridview column is returned but gives empty value

I have succeeded in retrieving Textbox from textbox column of Gridview, but it can't retain the value I have put there. It simply returns reference to textbox but miss the encapsulated value; Following is sample code I'm using to retrieve the textbox and its value; Please help me out of handling such problem.
protected void Submit_RowCommand(object sender, GridViewCommandEventArgs e)
{
int RowIndex = int.Parse(e.CommandArgument.ToString());
string userId = ApprovalParentGridView.DataKeys[RowIndex]["UserID"].ToString();
GridViewRow row = ApprovalParentGridView.Rows[RowIndex];
TextBox remarks = (TextBox)row.Cells[6].FindControl("txtRemark");
string remarks = remarks.Text.Trim();
...
}
Thanks in advance!!!
I have had this in the past and found it to be because the Gridview is loaded in Page_Load and the textbox had its value set in that routine, and Submit_RowCommand is fired afterwards.
You need to rebuild the gridview so that the control is in the controls collection but you need to be careful that you do not put the value in the textbox at the same time or else the code behind will overwrite the value that you user has entered from their browser.

Hide DataList row based on value from web service

I am retrieving a set of value from a web service and populating a dataList with those values-
ActiveDataList.DataSource = ws.TermsReturnActive(sql);
ActiveDataList.DataBind();
How can I hide a particular column of the dataList depending on the value,
e.g.
if(value == 1)
{
//Hide Column
}
This action however would have to hide the same row of another dataList in parallel to it.
I can modify a cell on this second dataList using by retrieving the value from the first as so -
TextBox tb1 = (TextBox)sender;
DataListItem item1 = (DataListItem)tb1.NamingContainer;
TextBox txt1 = (TextBox)tData.Items[item1.ItemIndex].FindControl("tTextBox");
string term = txt1.Text;
So if I can retrieve a value from a separate dataList row, I was thinking I would also be able to adjust its visability.
How can I achieve this as the web service call is done in the page load so I believe it would have to be done when the dataList item is bound?
if iam right, you should have something like this in your aspx-file right?:
<asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound"></asp:DataList>
As you can see you should add a "OnItemDataBound" Event where you can check your value and hide a item, if you want.
So you can react like this and hide some items:
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
TextBox tbCurrentTextBox = (TextBox)e.Item.FindControl("tTextBox");
if (DataList1.Items[e.Item.ItemIndex].ToString() == "1")
{
e.Item.Visible = false;
}
}
}

Control visible depending on what exists in the database

what I need help with is: I have a field in the database (MySql) called seq_orcamento that allow null, when it is not null I need that a LinkButton inside a FormView(that has as datasource a SqlDataSource) to be visible. What I did in my select command is
SELECT CASE seq_orcamento WHEN NOT NULL THEN '1' ELSE '0' END AS idc_seq FROM log_transacao
It is working fine, but is there a way to do some kind of bind that when idc_seq = 1 the LinkButton becomes visible?
I have no problem using code-behind if any of it is necessary, I'm using C#
You can use DataBound event of FromView Control
protected void FormView1_DataBound(object sender, EventArgs e)
{
DataRowView dataRow = ((DataRowView)FormView1.DataItem);
LinkButton lb= (LinkButton )FormView1.FindControl("LinkButton ");
if (Convert.ToBool(DataBinder.Eval(formview.DataItem, "idc_seq") ) )
{ lb.visible=true; }
else{lb.visible=false;}
}
I found a way to do it: in the Visible propertie I added the code
Visible='<%# Eval("idc_seq") == "1" ? true : false %>'
Don't know if it works yet because I need to use it only when the site is published, I will try it later

Asp.Net ListView passing parameters from a TextBox

Hi folks i have this terrible problems i hope any of you could help me first of all i have a ListView which has (lets say) 3 columns Id, Quantity, And an image button with an action.
the image button launch the function createparameter that splits the string into 2 int variables, but i want to get the first 1 from the textbox QuantityTextBox, What can i do to send the value of the textbox to the function, any ideas?
protected string CreateParameter(object arg1, object arg2)
{
return string.Concat(arg1, "|", arg2);
}
i wish there's a better way to do this than do a foreach of every list item and then find control because i dont care the other list items,
What you want to do will not work the way you want because the value of the textbox could be changed by the user. So it's not reasonable to try and bind it to the CommandArgument at runtime because there is no value there yet.
But when you handle the ItemCommand event for the ListView, you have access to which ListViewItem the button was clicked in, so you can use FindControl to get the textbox and look at its value.
For that to work, you will need to add a CommandName value to your ImageButton controls. And you would just pass in the product id in the command argument directly.
So, your button would look like this:
<asp:ImageButton ID="btnAdd" runat="server" Tooltip="Aggregar producto" CommandName="AddProduct"
CommandArgument="<%# Eval("IdProduct") %>" />
And your codebehind would look something like this (not tested):
protected void lv_Productos_OnItemCommand(object sender, ListViewCommandEventArgs e)
{
if (String.Equals(e.CommandName, "AddProduct"))
{
// Get a reference to your textbox in this item
TextBox textbox = e.Item.FindControl("QuantityTextBox") as TextBox;
if (textbox != null)
{
int quant = 0;
if (int.TryParse(textbox.Value, quant))
{
int prodId = (int)e.CommandArgument;
//do what you want with the quantity and product id
}
}
}
}

Gridview with FileUpload control

I have a gridview that shows an image as part of one of its columns. In Edit mode, I would like to let the user have the ability to upload a new image file, so I am using the FileUpload control in the edit portion of the template.
I have an event to capture this i believe:
protected void GridVew1_RowUpdated(object sender, GridViewUpdateEventArgs e)
{
if (FileUpload1.HasFile)
{
FileUpload1.SaveAs(Server.MapPath("images/hardware/" + FileUpload1.FileName));
}
}
I do not know how to call the control correctly though... How is this functionality coded?
First you need to handle the RowUpdating event instead of RowUpdated. Then you need to find a reference to the FileUpload control on that row.
IMPORTANT: You need to know the ordinal position of the column where the control is located. In my example, I set it to 0, assuming it is the first column. Otherwise, you would need to through the Cells collection to find it.
protected void gridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = gridView.Rows[e.RowIndex];
FileUpload fileUpload = row.Cells[0].FindControl("fileUpload1") as FileUpload;
if (fileUpload != null && fileUpload.HasFile)
{
fileUpload.SaveAs(Server.MapPath("images/hardware/" + fileUpload.FileName));
}
}
If I understand what you are doing here you will have to find the control in the row
so in VB some thing like this
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim aRow As GridViewRow = Me.GridView1.Rows(e.RowIndex)
dim xFileUpload as fileupload = CType(aRow.FindControl("FileUpload1"), FileUpload)
xFileUpload. save file etc etc etc
End Sub
Caveat - if this is wrong I would love to see a better way to do this!

Categories

Resources