Gridview Linkbutton Code behind - asp.net

Until now, I was working with VS 2003 and recently migrated to VS 2008. I am facing some peculiar problems.
In Vs 2003,I had a Datagrid, and one of the field was ButtonField(Link button). It was not a template field. The user clicks on the field and some data gets generated.
I have written a code, in Vb, like this, on dg_ItemCommand:
Strid = Ctype(e.commandsource,linkbutton).text
Now i want to use same method,for the gridview (I think datagrid is gridview in 2008). I wrote a code like this on dg_Rowcomand
Private Sub dgSampleCustomer_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles dgSampleCustomer.RowCommand
Try
Dim strid As String
Dim i As Integer
strid = CType(e.CommandSource, LinkButton).Text
...
It is throwing a error.
Unable to cast object of type 'System.Web.UI.WebControls.GridView' to
type 'System.Web.UI.WebControls.ButtonField'.
Can anybody help me out!

It looks like the source of the command is the GridView itself, not the button you are clicking. What you probably want to do is set this value you are looking for in the "CommandArgument" property of the Linkbutton. The markup would look something like this:
<asp:LinkButton ID="myLinkButton" runat="server"
CommandName="MyCommandName"
CommandArgument="MySpecialValue"
Text="Click Me" />
Then in the event you would simply:
' strid = "MySpecialValue"
strid = e.CommandArgument.ToString()
Instead of pulling the ID from the name of the control, you can now easily get it from the command. CommandName is optional in this particular case, but comes in handy if you have multiple buttons on a grid that do different things, such as "Edit" and "Delete". Then you can use the command name to handle each command in their own way in the same event:
If (e.CommandName = "Edit") Then
' Do Some Edit Code
End If

I'm wondering why you are trying to cast the command source to a LinkButton? If you would like to attach or otherwise send some kind of row-specific information to your button handler, you are able to do this with the CommandName and CommandArgument attributes of the ButtonField.
Like:
<asp:Gridview ID="...">
...
<columns>
<asp:buttonfield buttontype="Link"
commandname="Generate"
text="Generate"/>
...
</columns>
</asp:GridView>
This will be retrievable in the event handler by using:
if(e.CommandName=="Generate")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
string rowIndex = Convert.ToInt32(e.CommandArgument);
...
}
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx
UPDATE: (use DataKeys)
Since e.CommandArgument returns a row index, and you want the ID, use the DataKeys collection, first add your ID column to the DataKeyNames collection...
<asp:GridView ... DataKeyNames="ID">
... and then retrieve the values from the DataKeys collection, like:
GridView sourceGridView = (GridView) e.CommandSource;
rowIndex = Convert.ToInt32(e.CommandArgument);
strID = sourceGridView.DataKeys[rowIndex]["ID"];

You can try this in your rowcommand event
Dim index = Convert.ToInt32(e.CommandArgument)
Dim row = dg.Rows(index)
'find your linkbutton in template field (replace "lnkBtn" with your's)
Dim myLinkButton = CType(row.FindControl("lnkBtn"), LinkButton)
Dim strid As String = myLinkButton.Text
Let me know if it helps.

Related

ASP.Net DevExpress Gridview exporter CSV download with hyperlink link to website

I'm using DevExpress Gridview exporter to export Gridview to CSV and I wanted the sno field name's row data to become hyperlink and when I click on that it will show webpage of
https://xxx?sno_id
The CSV is exported with hyperlink on sno field name but I have no idea on how to make it works as what I wanted.
I have referred to this site, but still the same outcome.
ASP.net
<dx:ASPxGridViewExporter GridViewID="FeedbackGrid" ID="exportFeedbackGrid" onrenderbrick="exportFeedbackGrid_RenderBrick" runat="server">
</dx:ASPxGridViewExporter>
<dx:ASPxGridView ID="FeedbackGrid" ClientIDMode="Static" OnCustomUnboundColumnData="FeedbackGrid_CustomUnboundColumnData" ClientInstanceName="FeedbackGrid" runat="server" AutoGenerateColumns="False">
<Columns>
<dx:GridViewDataHyperLinkColumn FieldName="sno" Visible="false" PropertiesHyperLinkEdit-NavigateUrlFormatString="Http://{0}">
</dx:GridViewDataHyperLinkColumn>
</Columns></dx:ASPxGridView>
Vb.net
Protected Sub FeedbackGridBut_Click(sender As Object, e As EventArgs)
FeedbackGrid.Columns("sno").Visible = True
exportFeedbackGrid.WriteXlsToResponse()
End Sub
Protected Sub ExportFeedbackGrid_RenderBrick(sender As Object, e As ASPxGridViewExportRenderingEventArgs) Handles exportFeedbackGrid.RenderBrick
Dim dataColumn As GridViewDataColumn = e.Column As GridViewDataColumn
If Not e.RowType = GridViewRowType.Data And dataColumn Is Nothing Then
e.TextValue = e.Value.ToString()
End If
End Sub
anyone has idea on this?
I think you want to display something else on column value but it will create hyperlink with sno column. I suggest you to check the below reference:
Hyperlink Reference to another column DevExpress Gridview ASP.Net Webform
e.g.
GridViewDataHyperLinkColumn colItemName = new GridViewDataHyperLinkColumn();
colItemName.FieldName = "sno";
colItemName.PropertiesHyperLinkEdit.NavigateUrlFormatString = "~/details.aspx?Device={0}";
colItemName.PropertiesHyperLinkEdit.TextFormatString = "{0}";
colItemName.PropertiesHyperLinkEdit.TextField = "AnotherColumnFieldName";
ASPxGridView1.Columns.Add(colItemName);
References:
ASPxGridView - How to customize HyperLink column
How to create and configure a HyperLink column at runtime
If does not help then provide more details about the required functionality.

how to pass textbox value in asp.net to a code on update click

I have an issue with the following code, the markup is as follows:
<asp:GridView ID="GridView" runat="server"
AutoGenerateEditButton="True"
OnRowDataBound="GridView_RowDataBound"
OnRowEditing="GridView_RowEditing"
OnRowUpdating="GridView_RowUpdating"
CssClass="gridv">
<Columns>
<asp:TemplateField HeaderText="ASN">
<ItemTemplate>
<asp:Label ID="lblASN" runat="server" Text='<% #Eval("ASN")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtASN" runat="server" Text='<%# Bind("ASN")%>' CssClass="form-control"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
...
However when I run this code to get the new changed values from the textboxes that were successfully generated and populated, I only get the initial values not the news that user has entered, the code behind this is:
Protected Sub GridView_RowUpdating(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView.RowUpdating
Try
Dim row As GridViewRow = GridView.Rows(e.RowIndex)
Dim ID As Integer = DirectCast(row.FindControl("txtID"), TextBox).Text
Dim sASN As String = DirectCast(row.FindControl("txtASN"), TextBox).Text
Dim sDescription As String = DirectCast(row.FindControl("txtDescription"), TextBox).Text
Dim sManufacturer As String = DirectCast(row.FindControl("ddlmanufacturer"), DropDownList).SelectedValue
GridView.EditIndex = -1
Catch
End Try
ShowEmpDetails()
End Sub
So when I click the update button I use a message box to write the variables above and the values that I get are the same ones that got initially written to the textboxes, not the text that the user has changed?
I have worked out this code from a similar example in which this works with no issues, I honestly can not figure out what I am doing wrong?
As requested Page_Load event is calling this function:
Private Sub ShowEmpDetails()
Dim query As String = "SELECT * from inventory.all_items"
Dim cmd As MySqlCommand
cmd = New MySqlCommand(query)
cmd.Connection = myConn
Dim sda As New MySqlDataAdapter
sda.SelectCommand = cmd
Dim dt As New DataTable
sda.Fill(dt)
GridView.DataSource = dt
GridView.DataBind()
End Sub
Ok... there are so many things wrong with your coding approach that I don't know where to begin. Sorry.
Lets' start again and I'll explain how to properly pass values between the DOM and your code-behind.
Firstly, you need to understand how the DOM populates and builds the HTML for the browser to know what's going on.
I would test your project in Firefox and use the Inspector tool (right-click wep page). That tool is gold and has saved my already bald-head from revealing my skull!! :-)
As you know, the GridView control binds both the "view" and "edit" portions of the control into the same code. I can see you have Eval() for the view portion of the control (or the mode of the control I should say) and you have Bind() for the Edit mode. That is good. I personally hate BoundControls, as you cannot really see what's going on under the hood.
Next, avoid using AutoPostBack like the plaque! It's just ugly.
Get familiar with AjaxControlToolKit (there are others too, but start with the Ajax), and the ASP:UpdatePanel.
So in your case something like this ...
<asp:UpdatePanel ID="upADDMAIN" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="GridView" runat="server"
AutoGenerateEditButton="True"
OnRowDataBound="GridView_RowDataBound"
OnRowEditing="GridView_RowEditing"
OnRowUpdating="GridView_RowUpdating"
CssClass="gridv">
Try and put as much functionality back into the defaults of the GridView control. So go back to your DESIGNER mode in VStudio and add all the functionality you need like EDIT, UPDATE, DELETE, etc in the design mode of the GridView. This will also make sure your SQLDataSource is updated at the same time with the right SQL for the task.
Now why are you using OnRowEditing and OnRowUpdating?
My rule-of-thumb is always to keep things to a minimum and give as much control to ASP.net as possible. This avoids re-inventing the wheel with code-behind stuff that ASP.net can handle straight out of the box.
I generally use OnDataBound(), OnRowDataBound(), and OnRowUpdating() to both read the data and pre-UPDATE the data before the Update() gets called by the controls.
ie:
protected void gvLogins_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow gvRow = (GridViewRow)e.Row;
{
if (gvRow.RowType == DataControlRowType.DataRow)
{
and
protected void gvLogins_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//apply values to the SQL parameters for UPDATE, etc
GridViewRow gvRow = (GridViewRow)gvLogins.Rows[e.RowIndex];
to do some pre-Update updates outside of the GridView for example.
I never do prerendering or preloading of data in the PageLoad(). That is just re-inventing the wheel, when by default most ASP.net controls already have connectivity and updating built in!
Oh and to get the values of controls inside a GridView... just use FindControl() but in the right place! ie: the DataBound() events etc.
DropDownList ddlAgent = (DropDownList)gvRow.FindControl("ddlAgent");
HiddenField hfAgentID = (HiddenField)gvRow.FindControl("hfAgentID"); //from overall state,as EDIT mode defaults the hfAgentID to 0!
if (ddlAgent != null && hfAgentID != null)
ddlAgent.SelectedValue = hfAgentID.Value;
Sorry I only use C#, not VB.
Good luck.

color styling tablecell based upon value

I am trying to style tablecells based upon the text value of the cell. This is being done on specific fields to draw user attention These tablecells are hosted inside of a formview with an itemTemplate of asp:table object. The cells have an ID and are set to run at the server side so that I can grab them from codebehind.
I attempted to hook into the individual cells by handling the onDatabound, onPrerender, onLoad events of the tablecell. onPreRender is used in my code snippet but results were same on all three tablecell events. In handling these events I always had a string.empty for the text property instead of the actual value (even after databind was called). Because I didn't have the actual value my comparisons did not pass and my color styling did not get set as desired.
edit: I also tried handling the databound event of my formview to get to my cell values. No luck there either.
Here's relevant markup:
<asp:FormView ID="fvShipments" runat="server" DefaultMode="readonly">
<ItemTemplate>
<asp:Table ID="tblShipments" runat="server" GridLines="Both">
<asp:TableRow runat="server">
<asp:TableCell runat="server" ID="tbcCartonSizeOverride" CssClass="table-displayrow centerText" OnPreRender="tbcCartonSizeOverride_PreRender"><%#Eval("CartonSizeOverRide")%></asp:TableCell>
Here's relevant codebehind
Protected Sub tbcCartonSizeOverride_PreRender(sender As Object, e As EventArgs)
markAflag(CType(sender, TableCell))
End Sub
Private Sub markAflag(ByRef cell As TableCell)
If cell.Text.Trim.Length > 0 Then
cell.BackColor = Drawing.Color.Orange
Else
cell.BackColor = Drawing.Color.White
End If
End Sub
I'm not understanding why this isn't working. Unless my Eval markup solves AFTER prerender event?
Thanks for reading. I don't work with asp often so it's possible I have something simple confused here.
Ok I solved this issue by setting some datakeys on my formview object and doing a findcontrol call at runtime to get a reference to my table on the page. Here's some sample code that demonstrates what I did.
Private Sub colorReturncode()
Dim schedreturncode As Integer
schedreturncode = FormView1.DataKey(1)
Dim qtable As New Table
qtable = CType(FormView1.FindControl("table1"), Table)
If schedreturncode = 0 Then
qtable.Rows(4).Cells(2).BackColor = System.Drawing.Color.Green
qtable.Rows(4).Cells(2).ForeColor = System.Drawing.Color.White
ElseIf schedreturncode = -1 Then
qtable.Rows(4).Cells(2).BackColor = System.Drawing.Color.White
Else
qtable.Rows(4).Cells(2).BackColor = System.Drawing.Color.Red
qtable.Rows(4).Cells(2).ForeColor = System.Drawing.Color.White
End If
End Sub

How to retrieve value from sqldatasource1 to textbox1 using vb.net?

How to retrieve value from sqldatasource1 to textbox1 using vb.net ?
i have a table with field Employee Id : 1001
I wannna retrive the top1 employee id in textbox1 using sqldatasource1
Something like this may help:
DataView oDataView = new DataView();
oDataView = SqlDataSource1.Select(DataSourceSelectArguments.Empty);
DataRowView dr = oDataView[0];
TextBox1.Text = dr["EmployeeID"].ToString();
I have not tested this code though.
You may also want to read the followings to get more info on SQLDataSource:
http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/sqldatasource.aspx
http://www.aspfree.com/c/a/ASP.NET/Programming-the-ASPNET-20-SqlDataSource-Control/
http://www.defaultdotaspx.com/Answer.aspx?QuestionType=ASPNET&QuestionID=149
http://www.mikesdotnetting.com/Article/64/Bind-Data-From-a-SqlDataSource-to-a-Label
Hope this helps!
The easiest way is to call the data source's Select() method, pass in the arguments, and the results are returned as an IENumerable.
Otherwise, the only way is put the textbox in a FormView control, and specify the value to bind via Text='<%# Eval("Field") %>' /> and specify the DataSourceID of the form view.
HTH.

DropDownList SelectedIndex value not updating on AutoPostback

It looks like this question was addressed here, but his solution did not work for me. I am creating a dynamic dropdown menu system that populates a secondary dropdownlist with the results of a query based on the selected item in the first dropdown.
First dropdown getting populated:
Dim db As New linqclassesDataContext
Dim categories = (From c In db.faq_cats)
NewFaqDropDownCategory.DataSource = categories
NewFaqDropDownCategory.DataTextField = "category"
NewFaqDropDownCategory.DataValueField = "category_id"
NewFaqDropDownCategory.DataBind()
Unset(categories)
Unset(db)
Second dropdown getting populated:
Protected Sub NewFaqDropDownCategory_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim temp As Integer = CInt(Val(NewFaqDropDownCategory.SelectedIndex))
MsgBox(theDrop.SelectedValue)
Return
'Dim db As New linqclassesDataContext
'Dim faqs = (From f In db.faqs Where f.category = NewFaqDropDownCategory.SelectedValue)
'NewFaqDropDownList.DataSource = faqs
'NewFaqDropDownList.DataTextField = "question"
'NewFaqDropDownList.DataValueField = "id"
'NewFaqDropDownList.DataBind()
'NewFaqLabel.Visible = True
'NewFaqDropDownList.Visible = True
'Unset(faqs)
'Unset(db)
End Sub
The markup for the first dropdown...
<asp:DropDownList ID="NewFaqDropDownCategory" AutoPostBack="true" runat="server" OnSelectedIndexChanged="NewFaqDropDownCategory_SelectedIndexChanged">
</asp:DropDownList>
And the second...
<asp:DropDownList ID="NewFaqDropDownList" runat="server" Visible="false">
</asp:DropDownList>
No matter what I have tried, I always get "1" (the value of the first item in the second dropdown). The post I referenced earlier said this had to do with AutoPostBack and the server not knowing the list was updated yet.
Can anyone clarify this for me a little more?
Set a break point on the line that reads: NewFaqDropDownCategory.DataBind() and one in your event handler (NewFaqDropDownCategory_SelectedIndexChanged).
I suspect the databind is being called right before your NewFaqDropDownCategory_SelectedIndexChanged event fires causing your selected value to change.
If so, you need either to make sure you only databind if you aren't in the middle of your autopostback or instead of using NewFaqDropDownCategory.SelectedIndex on the first line of your event handler you can cast the sender parameter to a DropDownList and use its selected value.
I had the same problem. Found I forgot to look if I was posting back to the page or not and I was binding my DropDownList control in the Page_Load event of the page.
I had forgot to use:
if (!IsPostBack)
{
.... do databind ....
}
I think there is a bug in your LINQ query for the second drop down box
Dim faqs = (From f In db.faqs Where f.category = NewFaqDropDownCategory.SelectedValue)
Here you are comparing SelectedValue to category. Yet in the first combobox you said that the DataValueField should be category_id. Try changing f.category to f.category_id

Resources