I've a gridview with selected columns and rows. The row consists of Textbox for every column in a row. I need to select a row in order to get the current rowindex which I've done using below code.
protected void gvtotqty_onrowdatabound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvtotqty, "Select$" + e.Row.RowIndex);
e.Row.ToolTip = "Click to select this row.";
}
}
protected void gvtotqty_onselectedindexchanged(object sender, EventArgs e)
{
foreach (GridViewRow row in gvtotqty.Rows)
{
if (row.RowIndex == gvtotqty.SelectedIndex)
{
Session["rowindex"]=row.RowIndex;
row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
row.ToolTip = string.Empty;
}
else
{
row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
row.ToolTip = "Click to select this row.";
}
}
}
Everything is working fine where I get my rowindex when debugged but I am not able to type anything in Textbox as it is getting refreshed. I know that
Page.ClientScript.GetPostBackClientHyperlink
calls PostBackEvent. By using this method, how can I type the values in Textboxes?
Try with this line of code
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink((Control)sender, "Select$" + e.Row.RowIndex);
instead of your
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvtotqty, "Select$" + e.Row.RowIndex);
if it doesn't work try putting your grid inside an updatePanel and add the trigger for selected index change of your grid
<asp:UpdatePanel ID="uppan1" runat="server">
<ContentTemplate>
//yourGridView here with id=GridView1
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" EventName="selectedIndexChanged"/>
</Triggers>
</asp:UpdatePanel>
and change your selected index change function in this way
protected void gvtotqty_onselectedindexchanged(object sender, EventArgs e)
{
foreach (GridViewRow row in gvtotqty.Rows)
{
if (row.RowIndex == gvtotqty.SelectedIndex)
{
Session["rowindex"]=row.RowIndex;
row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
row.ToolTip = string.Empty;
row.Attributes["onclick"] = "";
}
else
{
row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
row.ToolTip = "Click to select this row.";
row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvtotqty, "Select$" + row.RowIndex);
}
}
}
As from your code I understand that you want to select the row and display the row in different color on selectedindex changed. If I am correct you can achieve this using the following method.
Add a button field, or template field, which contains a button with the command name "Select". Command name should be select.
<asp:ButtonField CommandName="Select" Text="Select"/>
Then for the grid view add SelectedRowStyle
<SelectedRowStyle BackColor="#A1DCF2" />
This should already select the row with differnt color. You dont need to write code for gvtotqty_onselectedindexchanged and gvtotqty_onrowdatabound
UPDATE
Based on your comment, since you may have multiple controls on the row, like textbox, link etc. then eventhough click the textbox to enter text, it does the postback and clears the content of the text.
I think you can use JavaScript for selecting row, instead of posting back as below.
<script type="text/javascript">
function selectRow(control) {
//Clear all the currently selected rows. change the scope, if you have multiple tables
$('tr').css("background-color", "#FFFFFF");
// Select the current row
$(control).css("background-color", "#A1DCF2");
}
</script>
Then add this function to the row in your onrowdatabound event
protected void gvtotqty_onrowdatabound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] = "selectRow(this);";
e.Row.ToolTip = "Click to select this row.";
}
}
Related
Using grid view binding it from code behind:
I want to bind a particular column data into a hyper link so when it clicked it should do a download.
How to do that ?
Below is my code :
for (int i = 0; i <= tbl.Columns.Count - 1; i++)
{
Telerik.Web.UI.GridBoundColumn boundfield = new Telerik.Web.UI.GridBoundColumn();
if (tbl.Columns[i].ColumnName.ToString() == "Row")
{
LinkButton lkbtn = new LinkButton();
lkbtn.CommandName = i;
lkbtn.CommandArgument = "dwnld";
lkbtn.Font.Underline = true;
lkbtn.Text = tbl.Columns(i).ColumnName.ToString();
boundfield.DataField = tbl.Columns(i).ColumnName.ToString()
boundfield.HeaderText = tbl.Columns(i).ColumnName.ToString();
GridView2.MasterTableView.Columns.Add(boundfield);
}
}
Why not use grid template column with link button.
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="btnDownload" OnClick="btnDownload_Click" runat="server">Download Something</asp:LinkButton>
</ItemTemplate>
</telerik:GridTemplateColumn>
protected void btnDownload_Click(object sender, EventArgs e)
{
LinkButton lbBtn = sender as LinkButton;
GridDataItem item = (GridDataItem)(sender as LinkButton).NamingContainer;
// Use item to get other details
...
...
}
Here is my code for grid row delete using link button but after click its deleted data after page refresh I want delete data on the page without refresh the page i also put update panel in my grid here is my code
protected void gvContent_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="modify")
{
GridViewRow row =
(GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
// this find the index of row:
int RowIndex = row.RowIndex;
//this store the value in varName1:
int id = Convert.ToInt32(
((Label)row.FindControl("lblContentId")).Text.ToString());
Response.Redirect("ContentManage.aspx?ContentId=" +Convert.ToInt32(id));
}
if (e.CommandName == "delete")
{
GridViewRow row = (GridViewRow)
(((LinkButton)e.CommandSource).NamingContainer);
// this finds the index of row:
int RowIndex = row.RowIndex;
//this stores the value in varName1:
int id = Convert.ToInt32(
((Label)row.FindControl("lblContentId")).Text.ToString());
Content_Data.DeleteContentDetails(id);
BindGrid();
UpdatePanel1.Update();
}
You would need to register gvContentas an async post-back control using the followin
void Page_Load()
{
if (!IsPostBack)
{
ScriptManager1.RegisterAsyncPostBackControl(gvContent);
}
}
This will cause your Grid to do async post-backs.
Hope that helps
Try adding an empty RowDeleting event
protected void gvContent_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
I have a custom grid on which i have binded data in my c# code behind. I have given a hyperlink field to one of my column. If i click the hyperlink value, it should navigate to the details page of that hyperlink value. The code is given below,
protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink myLink = new HyperLink();
myLink.Text = e.Row.Cells[2].Text;
e.Row.Cells[2].Controls.Add(myLink);
myLink.NavigateUrl = "Estimation.aspx?EstimateID=" + EstimateID + "&VersionNo=" + VersionNo;
}
}
If i click the link, the page is getting navigated, but i am not getting the details which are already pre-loaded in that page. Please give me suggestions on how to incorporate this.
Thanks
You can use this to redirect, read this
<asp:HyperLink ID="HyperLink1"
runat="server"
NavigateUrl="Default2.aspx">
HyperLink
</asp:HyperLink>
to add attribute with link just add
HyperLink1.Attributes.Add ("");
You need to do a small change in the RowDataBound event
myLink.Attributes.Add("href"," your url");
You need to fetch the values for EstimateID and VersionNo from the grid row data. Take a look at the documentation for GridViewRowEventArgs and you'll see there's a .Row property.
So your code needs to be something like:
myLink.NavigateUrl = "Estimation.aspx?EstimateID=" + e.Row.Cells[4].Text + "&VersionNo=" + e.Row.Cells[5].Text;
Or, maybe you need to get to the data item associated with the grid row, in which case take a look at e.Row.DataItem, the GridViewRow.DataItem property. This DataItem will need to be cast to the type of data you've bound to the grid in order to fetch the data from it, which might be something like:
((MyCustomDataRow)e.Row.DataItem).EstimateID
Try below solution :
Page-1 that is your list page :
ASPX code :
<asp:GridView ID="GridView1" runat="server"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server">HyperLink</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind :
protected void Page_Load(object sender, EventArgs e)
{
List<Data> lstData = new List<Data>();
for (int index = 0; index < 10; index++)
{
Data objData = new Data();
objData.EstimateID = index;
objData.VersionNo = "VersionNo" + index;
lstData.Add(objData);
}
GridView1.DataSource = lstData;
GridView1.DataBind();
}
public class Data
{
public int EstimateID { get; set; }
public string VersionNo { get; set; }
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink HyperLink1 = e.Row.FindControl("HyperLink1") as HyperLink;
HyperLink1.NavigateUrl = "Details.aspx?EstimateID=" + e.Row.Cells[1].Text + "&VersionNo=" + e.Row.Cells[2].Text;
}
}
Page-2 that is your details page :
Code behind :
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Request.QueryString["EstimateID"].ToString());
Response.Write(Request.QueryString["VersionNo"].ToString());
}
I created a dynamic button inside c# code and I have assigned some value in button.text but the problem is that on buttn_Click event I want to show details related to that value. So any idea how to do this?
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < list.Count; i++)
{
lnk1 = new Button();
VW obj1 = list[i];
lnk1.Text = " "+obj1.ticketNo+": "+obj1.subject+": "+obj1.qu;
lnk1.Click += new EventHandler(lnk1_Click);
}
}
I want to show above mentioned obj1.ticketno in next page like ticket No: some value is selected
You could retrieve the reference to the button using the sender parameter of the event handler and cast the value to the Button type.
In lnk1_Click event handler you can get the link by type casting the sender to Button type and get the link text. Using that you can retrieve ticket number for which click has been done.
Something like this:
void lnk1_Click(object sender)
{
Button clickedLinkButton = sender as Button;
String buttonText = clickedLinkButton .Text;
String clickedTicketNumber =
buttonText
.SubString(0, buttonText.IndexOf(':'))
.Trim();"
}
Here is the sample code segment
protected void lnk1_Click(object sender, EventArgs e)
{
Button bt = sender as Button;
bt.Text;
}
you can use GridView or Repeater and in Iteme Template you can put button. and bind perticular grid or repeater.
<asp:repeater runat="server" id="rpt">
</ItemTemplate>
<asp:LinkButton runat="serevr" ID="lbtnLInkButton" CommandArgument='<%#Eval("ID") %>' CommandName="Edit" OnClick="lbtnLInkButton_Click">"+<%#Eval("ticketNo")%> <%#Eval("subject")%> <%#Eval("qu")%>
</ItemTemplate>
</asp:repeater>
Bind This Repeater to Datatable or make Dummy DataTable and bind it.
DataTable dt = new DataTable();
dt.Columns.Add("ticketNo");
dt.Columns.Add("Subject");
dt.Columns.Add("qu");
for (int i = 0; i < list.Count; i++)
{
dt.Rows.Add(new object[] { "Ticket Number Value", "Subject Value", "qu Value"});
}
rpt.DataSource = dt;
rpt.DAtabind();
You can get button event like this
protected void lbtnLInkButton_Click(object sender, EventArgs e)
{
int i = Convert.ToInt32(((LinkButton)sender).CommandArgument);
}
***Note : I have writtern the code extempore and not rested it on Visual Studio so there May be Some Spelling Mistakes.**
I have a gridview in an update panel with the following code to select a row, this in turn updates another updatepanel with details from the form record.
protected void gvMainGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Make the entire row clickable to select this record
//Uses javascript to post page back
e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(this.gvMainGrid, "Select$" + e.Row.RowIndex));
}
}
I am manually binding the gridview from a database and don't want to rebind the grid just to highlight the row, but I can't seem to add any javascript to the onclick event, it seems to either show the GetPostBackClientHyperlink or the row highlight javascript.
How to highlight gridview when row is selected
for this you have to write this code in your code behind file in OnRowCreated event or your can also write this code in OnRowDataBound event of grid...
protected void ctlGridView_OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "onGridViewRowSelected('" + e.Row.RowIndex.ToString() + "')");
}
}
and add this one script
<script language="javascript" type="text/javascript">
var gridViewCtlId = '<%=ctlGridView.ClientID%>';
var gridViewCtl = null;
var curSelRow = null;
function getGridViewControl()
{
if (null == gridViewCtl)
{
gridViewCtl = document.getElementById(gridViewCtlId);
}
}
function onGridViewRowSelected(rowIdx)
{
var selRow = getSelectedRow(rowIdx);
if (curSelRow != null)
{
curSelRow.style.backgroundColor = '#ffffff';
}
if (null != selRow)
{
curSelRow = selRow;
curSelRow.style.backgroundColor = '#ababab';
}
}
function getSelectedRow(rowIdx)
{
getGridViewControl();
if (null != gridViewCtl)
{
return gridViewCtl.rows[rowIdx];
}
return null;
}
</script>
and it will highlight the selected row..
I was struggling to add both on click events to the row databound:
e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(this.gvMainGrid, "Select$" + e.Row.RowIndex));
e.Row.Attributes.Add("onclick", "onGridViewRowSelected('" + e.Row.RowIndex.ToString() + "')");
Appending the PostBack select after the row highlight method with ';' seems to have worked.
e.Row.Attributes.Add("onclick", "onGridViewRowSelected('" + e.Row.RowIndex.ToString() + "');" + ClientScript.GetPostBackClientHyperlink(this.gvMainGrid, "Select$" + e.Row.RowIndex));
Firstly, you can't apply text-decoration to a <tr>... or a <td> for that matter. You need to apply it to the elements inside.
Here are a couple adjustments you can try-
e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand';";
e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(this.gvMainGrid.ClientId, "Select$" + e.Row.RowIndex));
The 1st works for me in code. Didn't have anything handy to test the 2nd.