How to pass the boundfield value to a label in the same page? - asp.net

In my aspx page, I have a detailsView and a label. The label's text should show the same value as a boundfield of the detailsview. How do I get both of them populated at the same time?
Following is my apsx page, I tried the Eval, it didn't work. I don't want to do it in the code-behind.
<tr>
<td > <asp:label runat="server" text='<%# Eval("ReporterName")%>'/></td>
</tr>
<tr>
<td>
<asp:DetailsView ID="DetailsView1" runat="server" >
<Fields>
<asp:BoundField DataField="sprID" HeaderText="SPRID" ReadOnly="True"
SortExpression="sprID" >
<HeaderStyle Width="230px" />
</asp:BoundField>
<asp:BoundField DataField="ProductName" HeaderText="Product"
SortExpression="ProductName" />
<asp:BoundField DataField="DivisionName" HeaderText="Technology Group"
SortExpression="DivisionName" />
<asp:BoundField DataField="DisciplineName" HeaderText="Discipline"
SortExpression="DisciplineName" />
<asp:BoundField DataField="ReporterName" HeaderText="Reporter"
SortExpression="ReporterName" />
<asp:BoundField DataField="OwnerName" HeaderText="Owner"
SortExpression="OwnerName" />
<asp:BoundField DataField="SalesLeadName" HeaderText="SalesLead"
SortExpression="SalesLeadName" />
<asp:BoundField DataField="RegionName" HeaderText="Region"
SortExpression="RegionName" />
</Fields>

Try using the DataBound event, like this:
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
Label1.Text = DataBinder.Eval(DetailsView1.DataItem, "SomeValue").ToString();
}

Related

gridview link button redirect to another page

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" EmptyDataText="Sevkiyat Bulunamadı"
class="table table-striped table-bordered table-condensed"
AllowPaging="True"
OnPageIndexChanged="GridView1_PageIndexChanged"
OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowDataBound="RowDataBound"
OnRowCommand="GridView1_RowCommand"
AllowSorting="True"
DataSourceID="SqlDataSource1"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="checkAll" runat="server" onclick="checkAll(this);" />
Teklif İste
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" onclick="Check_Click(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="yetkiliad" ItemStyle-CssClass="yetkiliad" HeaderText="yetkiliad" SortExpression="yetkiliad" />
<asp:BoundField DataField="yetkilisoyad" ItemStyle-CssClass="yetkilisoyad" HeaderText="yetkilisoyad" SortExpression="yetkilisoyad" />
<asp:BoundField DataField="firmaismi" ItemStyle-CssClass="firmaismi" HeaderText="firmaismi" SortExpression="firmaismi" />
<asp:BoundField DataField="telefon" ItemStyle-CssClass="telefon" HeaderText="telefon" SortExpression="telefon" />
<asp:BoundField DataField="adres" ItemStyle-CssClass="adres" HeaderText="adres" SortExpression="adres" />
<asp:BoundField DataField="mail" ItemStyle-CssClass="mail" HeaderText="mail" SortExpression="mail" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="View" ID="lnkView" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle VerticalAlign="Middle" />
</asp:GridView>
I have code like this. I cant find how to redirect when click view button on each row. I mean when i clicked second row's view it shoudl be redirected related link with second row. Could you help me about it
No need to create a LinkButton inside the asp:TemplateField. Just use an asp:HyperLinkField like this
<asp:HyperLinkField Text="View"
DataNavigateUrlFields="firmaname"
DataNavigateUrlFormatString="~/{0}.aspx" />
<asp:LinkButton ID="lnkView" runat="server" OnClick="lnkView_Click" CommandArgument='<%#Eval("Id") %>View</asp:LinkButton>
protected void lnkView_Click(object sender, EventArgs e)
{
int Id = (sender as LinkButton).CommandArgument;
Response.Redirect("Foo.aspx?Id=" + Id);
}

Adding a new column with buttons in gridview and binding data to it

I have added a button for Print in my gridview table as follows
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="SqlDataSource2"
onselectedindexchanged="GridView1_SelectedIndexChanged" Width="522px">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Story_number" HeaderText="Story_number"
SortExpression="Story_number" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
<asp:BoundField DataField="Memory_card" HeaderText="Memory_card"
SortExpression="Memory_card" />
<asp:BoundField DataField="Story_Name" HeaderText="Story_Name"
SortExpression="Story_Name" />
<asp:ButtonField ButtonType="Button" Text="print" />
</Columns>
</asp:GridView>
Please help me with the c# code for this button. When the button is pressed I need it to redirect to a page (print.aspx). I have been trying the following code but it does not work .Thanks in advance for your help.
Session["id"] = GridView1.SelectedRow.Cells[0].Text;
Response.Redirect("Print.aspx");
Instead of buttonField, use template field
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ButtonPrint" runat="server" CssClass="yourCssClassIsNeedIt" OnClick="printRegFunction"
CommandArgument='<%# Bind("id") %>' ImageUrl="images/button.png"/>
</ItemTemplate>
</asp:TemplateField>
your server code or behind code here:
protected void printRegFunction(object sender, ImageClickEventArgs e)
{
Session["id"] = ((ImageButton)sender).CommandArgument;
Response.Redirect("Print.aspx");
}

When using a GridView, SelectedIndex is always 0 when using custom select button

When clicking the select button in gvSquid2, x gets a valid and correct value. When clicking the select button in gvSquid, I get an ArgumentOutOfRange Exception because gv.SelectedIndex=0. Additionally, gv.SelectedDataKey is null. How do I get the DataKey information? Putting it in a column is not acceptable, as it must be hidden from the user. Hiding the column is useless, because during DataBind(), the value is discarded. I ripped this example from Microsoft's website. What am I missing?
<asp:gridview ID="gvSquid2" DataSourceID="dsComments" AutoGenerateColumns="false" AutoGenerateSelectButton="true" DataKeyNames="ID" OnSelectedIndexChanged="gvSquid_SelectedIndexChanged" runat="server" EnableViewState="false">
<Columns>
<asp:BoundField DataField="Date" ReadOnly="true" HeaderText="Date" SortExpression="Date" />
<asp:BoundField DataField="Comment" ReadOnly="true" HeaderText="Comment" SortExpression="Comment" />
<asp:BoundField DataField="Username" ReadOnly="true" HeaderText="User" SortExpression="Username" />
</Columns>
</asp:gridview>
<asp:gridview ID="gvSquid" DataSourceID="dsComments" AutoGenerateColumns="false" DataKeyNames="ID" OnSelectedIndexChanged="gvSquid_SelectedIndexChanged" runat="server" EnableViewState="false">
<Columns>
<asp:CommandField ButtonType="Image" HeaderText="Select" ShowSelectButton="true" SelectImageUrl="~/includes/RedX.jpg" />
<asp:BoundField DataField="Date" ReadOnly="true" HeaderText="Date" SortExpression="Date" />
<asp:BoundField DataField="Comment" ReadOnly="true" HeaderText="Comment" SortExpression="Comment" />
<asp:BoundField DataField="Username" ReadOnly="true" HeaderText="User" SortExpression="Username" />
</Columns>
</asp:gridview>
protected void gvSquid_SelectedIndexChanged(object sender, EventArgs e) {
string x;
x = gvSquid.DataKeys[gvSquid.SelectedIndex].Value.ToString();
}
Try using the SelectedRow of the grid view to get to the RowIndex, like this:
protected void gvSquid_SelectedIndexChanged(object sender, EventArgs e)
{
string x;
GridViewRow theGridViewRow = gvSquid.SelectedRow;
x = gvSquid.DataKeys[theGridViewRow.RowIndex].Value.ToString();
}
I figured it out. EnableViewState="true" has to be set. Once I changed that, suddenly the DataKeys appeared.
This question helped.

Show record in gridview based on dropdown list, fromdate and enddate entered in textbox

I need to show my data in a gridview based on my dropdown list, fromdate and todate when data is entered in the textbox. How can I do that? What I have done till now is I created two pages. In first page I have a dropdown list and a form. When I select value from drop-down list the form for the selected value appears and user can enter details in it and submits that form. After submitting the data store in the database. In second page I have a grid view where stored value can be seen, dropdown list, from date and to date textbox now what I want is to show the records in the gridview based on my dropdown selection, fromdate and todate.
Code for second form:
<asp:DropDownList ID="ddlPortal" runat="server" AutoPostBack="True"
onselectedindexchanged="ddlPortal_SelectedIndexChanged">
<asp:ListItem>TRAVELONG</asp:ListItem>
<asp:ListItem>ONETRAVEL</asp:ListItem>
<asp:ListItem>.UK-BSP</asp:ListItem>
<asp:ListItem>.CA-YYZ</asp:ListItem>
<asp:ListItem>.CA-YVR</asp:ListItem>
<asp:ListItem>Partial MCO Refund</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lbFrom" Text="From" runat="server" /> <asp:TextBox ID="tbFrom" runat="server" />
<asp:Label ID="lblto" Text="To" runat="server" /> <asp:TextBox ID="tbTo" runat="server" />
<asp:Button ID="btnSearch" runat="server" Text="Search"
onclick="btnSearch_Click" />
Gridview details:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" onrowcommand="GridView1_RowCommand"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Query">
<itemtemplate>
<asp:LinkButton CommandName="cmdBind" runat="server" Text='<%#Eval("ID")%>' ID="ID" ToolTip='<%#Eval("ID")%>'>LinkButton
</asp:LinkButton>
</itemtemplate>
</asp:TemplateField>
<asp:BoundField DataField="Portal" HeaderText="Portal" />
<asp:BoundField DataField="TID" HeaderText="TID" />
<asp:BoundField DataField="PNR" HeaderText="PNR" />
<asp:BoundField DataField="TicketNumber" HeaderText="Ticket Number" />
<asp:BoundField DataField="ESACCode" HeaderText="ESACCode" />
<asp:BoundField DataField="WaiverCode" HeaderText="WaiverCode" />
<asp:BoundField DataField="Remarks" HeaderText="Remarks" />
<asp:BoundField DataField="UnusedTicketAmount" HeaderText="UnusedTicketAmount" />
<asp:BoundField DataField="ddlUnusedAmount" HeaderText="ddlUnusedAmount" />
<asp:BoundField DataField="AirlinePenality" HeaderText="AirlinePenality" />
<asp:BoundField DataField="ddlAirlinePenality" HeaderText="ddlAirlinePenality" />
<asp:BoundField DataField="NetRefundProcess" HeaderText="NetRefundProcess" />
<asp:BoundField DataField="ddlNetRefundProcess" HeaderText="ddlNetRefundProcess" />
<asp:BoundField DataField="RefundableCommission" HeaderText="RefundableCommission" />
<asp:BoundField DataField="ddlRefundableCommission" HeaderText="ddlRefundableCommission" />
<asp:BoundField DataField="CouponRefunded" HeaderText="CouponRefunded" />
<asp:BoundField DataField="RefundType" HeaderText="RefundType" />
</Columns>
I want to write code on click event of button but I don't know how. Can you help me on this?
.cs file:
protected void btnSearch_Click(object sender, EventArgs e)
{
}
If you are using DataTable for binding your GridView then, you can filter the data & bind your GridView again.
You can use: DataView.RowFilter Property for filtering data.
Eg.:
DataTable dt= GetData();
DataView dv = dt.DefaultView;
dv.RowFilter = "Portal= '" + ddlPortal.SelectedValue.Trim() + "'"
GridView1.DataSource=dv;
GridView1.DataBind();
Also, you can use LINQ to filter data:
DataTable dt= GetData();
EnumerableRowCollection<DataRow> query =
from row in dt.AsEnumerable()
where row.Field<String>("Portal") == ddlPortal.SelectedValue.Trim()
select row;
DataView dv = query.AsDataView();
GridView1.DataSource=dv;
GridView1.DataBind();

Row command event of gridview is not firing for radiobuttinlist in asp.net

I have rdiobutton list in ItemTemplet field of gridview which is set ispostback=true but still radiobutton list is not firing row_command event for gridview.If I do same for button then it works fine. I have written code to bind the grid in (!IspostBack) but still the problem is
there..How can I get the rid out of these.
This the code for my Grid.
<asp:GridView ID="grdSalesPerson" runat="server" AutoGenerateColumns="False" BackColor="#DEBA84" OnRowCommand="grdSalesPerson_RowCommand">
<Columns>
<asp:BoundField DataField="SalesPersonID" HeaderText="SalesPersonID" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="UserName" HeaderText="User Name" />
<asp:BoundField DataField="StateName" HeaderText="State Name" />
<asp:BoundField DataField="CityName" HeaderText="City Name" />
<asp:TemplateField HeaderText="Is Active">
<ItemTemplate>
<asp:RadioButtonList ID="rbActive" runat="server" CommandName="IsAct" AutoPostBack="true">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This is the code for Row_Command Event
protected void grdSalesPerson_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
if (e.CommandName == "IsAct")
{
// GridViewRow row = (GridViewRow) (((Button)grdSalesPerson.FindControl("btnIsActive")).NamingContainer);
GridViewRow row = (GridViewRow)(((RadioButton)e.CommandSource).NamingContainer);
int i = Convert.ToInt32(row.Cells[0].Text);
}
}

Categories

Resources