.Net 2.0/3.5 sur Visual Studio 2010 - Windows Vista

Bonjour,

J'ai un gridview qui contient un bouton de type "select" (je l'ai nommé "Link"). Lorsque je click sur Link, je suis incapable d'obtenir le contenu d'une cellule de la rangée sélectionnée. J'ai pris le code directement sur MSDN et ca ne fonctionne pas. J'ai essayé beaucoup d'autres trucs mais sans succès.

L'évènement:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
    {
        GridViewRow row = GridView1.SelectedRow;
        string message = "You selected " + row.Cells[2].Text + ".";
    }
est lancé, l'objet row est bien actualisé mais le row.Cells[2].Text est vide!

.aspx
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewInsertEditUpdateDelete.aspx.cs" Inherits="GridViewInsertEditUpdateDelete" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
 
 
    <form id="form1" runat="server">
 
        <br />
        <asp:Label ID="Label9" runat="server" Text="Project"></asp:Label>
        : &nbsp;
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
 
    <div >
    <br />
 
 
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <div>
            <asp:Panel ID="Panel1" runat="server" Width="989px" Wrap="true">
             <asp:GridView ID="GridView1"   runat="server" AutoGenerateColumns="False" DataKeyNames="softwareRequirementId" 
        OnRowCancelingEdit="GridView1_RowCancelingEdit" 
        OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" 
        OnRowCommand="GridView1_RowCommand" 
            Onselectedindexchanged="GridView1_SelectedIndexChanged" Onselectedindexchanging="GridView1_SelectedIndexChanging" 
        ShowFooter="True" OnRowDeleting="GridView1_RowDeleting" 
        EnableModelValidation="True" Width="100%"> 
 
        <Columns> 
 
 
 
          <asp:TemplateField HeaderText="Id"  SortExpression="softwareRequirementId"> <EditItemTemplate> 
          <asp:TextBox ID="txtsoftwareRequirementId"  runat="server"  Text='<%# Eval("softwareRequirementId") %>'></asp:TextBox> 
        </EditItemTemplate> 
        <FooterTemplate> 
          <asp:TextBox ID="txtNewsoftwareRequirementId" runat="server"></asp:TextBox> </FooterTemplate> 
        <ItemTemplate> 
          <asp:Label ID="Label2" runat="server" Text='<%# Bind("softwareRequirementId") %>'></asp:Label> 
        </ItemTemplate> 
            <ControlStyle BackColor="GhostWhite" Font-Bold="True" Width="100px" />
              <FooterStyle Width="100px" />
            <HeaderStyle BackColor="Beige" Width="100px" />
              <ItemStyle Width="100px" ForeColor="#0099FF" />
        </asp:TemplateField> 
 
 
        <asp:TemplateField HeaderText="Project"  SortExpression="projectId"> <EditItemTemplate> 
          <asp:TextBox ID="txtprojectId" columns="1"  runat="server" Text='<%# Eval("projectId") %>'></asp:TextBox> 
        </EditItemTemplate> 
        <FooterTemplate> 
          <asp:TextBox ID="txtNewprojectId" runat="server"></asp:TextBox> </FooterTemplate> 
        <ItemTemplate> 
          <asp:Label ID="Label3" runat="server" Text='<%# Bind("projectId") %>'></asp:Label> 
        </ItemTemplate> 
            <ControlStyle BackColor="GhostWhite" Font-Bold="True" Width="100px" />
            <HeaderStyle BackColor="Beige" Width="100px" />
        </asp:TemplateField> 
 
        <%--<asp:TemplateField HeaderText="Elevate" ShowHeader="True"> 
            <EditItemTemplate> 
               <asp:LinkButton ID="LinkElevate" runat="server" CausesValidation="True" CommandName="elevate" Text="Elevate"></asp:LinkButton> 
            </EditItemTemplate>
            <HeaderStyle BackColor="Tan" />
 
        </asp:TemplateField> --%>
 
        <asp:ButtonField  ButtonType="Button" CommandName="elevate" Text="Button" />
 
 
 
        <asp:TemplateField HeaderText="Section" SortExpression="section"> <EditItemTemplate> 
          <asp:TextBox ID="txtsection" columns="1"  runat="server" Text='<%# Eval("section") %>'></asp:TextBox> 
        </EditItemTemplate> 
        <FooterTemplate> 
          <asp:TextBox ID="txtNewsection" runat="server"></asp:TextBox> </FooterTemplate> 
        <ItemTemplate> 
          <asp:Label ID="Label4" runat="server" Text='<%# Bind("section") %>'></asp:Label> 
        </ItemTemplate> 
            <HeaderStyle BackColor="Tan" />
        </asp:TemplateField> 
 
 
 
        <asp:TemplateField HeaderText="rowNumber"> 
        <EditItemTemplate> 
          <asp:TextBox ID="txtrowNumber" runat="server" Text='<%# Bind("rowNumber") %>'></asp:TextBox> 
        </EditItemTemplate> 
        <FooterTemplate> 
          <asp:TextBox ID="txtNewrowNumber" runat="server" ></asp:TextBox> 
        </FooterTemplate> 
        <ItemTemplate> 
          <asp:Label ID="Label5" runat="server" Text='<%# Bind("rowNumber") %>'></asp:Label> 
        </ItemTemplate> 
            <HeaderStyle BackColor="Tan" />
        </asp:TemplateField> 
 
 
 
        <asp:TemplateField HeaderText="softwareRequirement" > 
        <EditItemTemplate> 
          <asp:TextBox ID="txtsoftwareRequirement" TextMode="multiline" columns="50" runat="server" Text='<%# Bind("softwareRequirement") %>'></asp:TextBox> 
        </EditItemTemplate> 
        <FooterTemplate> 
          <asp:TextBox ID="txtNewsoftwareRequirement" runat="server" ></asp:TextBox> 
        </FooterTemplate> 
        <ItemTemplate> 
          <asp:Label ID="Label6" runat="server" Text='<%# Bind("softwareRequirement") %>'></asp:Label> 
        </ItemTemplate> 
            <HeaderStyle BackColor="Tan" />
        </asp:TemplateField> 
 
 
 
        <asp:TemplateField HeaderText="isTraced" SortExpression="isTraced"> 
        <EditItemTemplate> 
          <asp:TextBox ID="txtisTraced" runat="server" Text='<%# Bind("isTraced") %>'></asp:TextBox> 
        </EditItemTemplate> 
        <FooterTemplate> 
          <asp:TextBox ID="txtNewisTraced" runat="server" ></asp:TextBox> 
        </FooterTemplate> 
        <ItemTemplate> 
          <asp:Label ID="Label7" runat="server" Text='<%# Bind("isTraced") %>'></asp:Label> 
        </ItemTemplate> 
            <HeaderStyle BackColor="Tan" />
        </asp:TemplateField> 
 
 
 
 
        <asp:TemplateField HeaderText="image" > 
        <EditItemTemplate> 
          <asp:TextBox ID="txtimage" runat="server" Text='<%# Bind("image") %>'></asp:TextBox> 
        </EditItemTemplate> 
        <FooterTemplate> 
          <asp:TextBox ID="txtNewimage" runat="server" ></asp:TextBox> 
        </FooterTemplate> 
        <ItemTemplate> 
          <asp:Label ID="Label8" runat="server" Text='<%# Bind("image") %>'></asp:Label> 
        </ItemTemplate> 
            <HeaderStyle BackColor="Tan" />
        </asp:TemplateField> 
 
 
 
 
        <asp:TemplateField HeaderText="Edit" ShowHeader="False"> 
            <EditItemTemplate> 
                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton> 
                <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton> 
 
 
            </EditItemTemplate> 
 
            <FooterTemplate> 
                <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:LinkButton> 
            </FooterTemplate> 
 
            <ItemTemplate> 
                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton> 
 
            </ItemTemplate> 
 
            <HeaderStyle BackColor="Tan" />
 
        </asp:TemplateField> 
 
 
        <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" >
            <HeaderStyle BackColor="Tan" />
        </asp:CommandField> 
 
 
 
            <asp:ButtonField CommandName="Select" HeaderText="Link" ShowHeader="True" 
                Text="Link" />
 
 
 
        </Columns> 
</asp:GridView> 
 
            </asp:Panel>
        </div>
    </form>
</body>
</html>

.aspx.cs
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
 
public partial class GridViewInsertEditUpdateDelete : System.Web.UI.Page
{
    private string projectId;
    SoftwareRequirementsCls softwareRequirements = new SoftwareRequirementsCls();
    ProjectCls projects = new ProjectCls();
 
 
    protected void Page_Load(object sender, EventArgs e)
    {
        UrlParameterPasser urlWrapper = new UrlParameterPasser();
        projectId = Request.QueryString["projectId"];
 
        if (!IsPostBack)
        {
 
            DataTable dtProjects = projects.Fetch(projectId);
 
            foreach (DataRow row in dtProjects.Rows)
            {
                TextBox2.Text = row["projectName"].ToString();   
            }
 
            FillSoftwareRequirementsInGrid();
        }
 
 
 
    }
 
    private void FillSoftwareRequirementsInGrid()
    {
        DataTable dtSoftwareRequirements = softwareRequirements.Fetch(projectId);
 
        if (dtSoftwareRequirements.Rows.Count > 0)
        {
            GridView1.DataSource = dtSoftwareRequirements;
            GridView1.DataBind();
        }
        else
        {
            dtSoftwareRequirements.Rows.Add(dtSoftwareRequirements.NewRow());
            GridView1.DataSource = dtSoftwareRequirements;
            GridView1.DataBind();
 
            int TotalColumns = GridView1.Rows[0].Cells.Count;
            GridView1.Rows[0].Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = TotalColumns;
            GridView1.Rows[0].Cells[0].Text = "No Record Found";
        }
    }
 
 
 
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
            TextBox txtNewsoftwareRequirementId = (TextBox)GridView1.FooterRow.FindControl("txtNewsoftwareRequirementId");
            TextBox txtNewprojectId = (TextBox)GridView1.FooterRow.FindControl("txtNewprojectId");
            TextBox txtNewsection = (TextBox)GridView1.FooterRow.FindControl("txtNewsection");
            //DropDownList cmbNewsection = (DropDownList)GridView1.FooterRow.FindControl("cmbNewsection");
            TextBox txtNewrowNumber = (TextBox)GridView1.FooterRow.FindControl("txtNewrowNumber");
            TextBox txtNewsoftwareRequirement = (TextBox)GridView1.FooterRow.FindControl("txtNewsoftwareRequirement");
            TextBox txtNewisTraced = (TextBox)GridView1.FooterRow.FindControl("txtNewisTraced");
            TextBox txtNewimage = (TextBox)GridView1.FooterRow.FindControl("txtNewimage");
 
            softwareRequirements.Insert(projectId, txtNewsection.Text, txtNewrowNumber.Text, txtNewsoftwareRequirement.Text, txtNewisTraced.Text, txtNewimage.Text);
            FillSoftwareRequirementsInGrid();
        }
 
        if (e.CommandName.Equals("Edit"))
        {
 
 
        }
 
        if (e.CommandName.Equals("Link"))
        {
 
 
 
            //UrlParameterPasser urlWrapper = new UrlParameterPasser("linkToSysReq.aspx");
 
            //urlWrapper["softwareRequirementId"] = GridView1.Rows[index].Cells[0].Text;
            //urlWrapper["projectId"] = GridView1.Rows[index].Cells[1].Text;
            //urlWrapper.PassParameters();     
 
        }
        else if (e.CommandName.Equals("elevate"))
        {
            //int index = Convert.ToInt32(e.CommandArgument);
 
            //GridView1.Rows[index].Cells[2].Text = "4";
 
            //string a = GridView1.Rows[index].Cells[2].Text;
 
            //int softwareRequirementId = Convert.ToInt32(GridView1.Rows[index].Cells[3].Text);
 
             //GridViewRow row = GridView1.SelectedRow;
 
             //int a = row.RowIndex;
 
            //string sec = txtNewsection.Text;
 
 
            //int softwareRequirementId = Convert.ToInt32(GridView1.Cells[0].Text);
            //int a = Convert.ToInt32(GridView1.Rows[index].Cells[3].Text);
            //int a++;
            //GridView1.Rows[index].Cells[1].Text = a.ToString();
        }
 
 
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
 
                //    DropDownList cmbType = (DropDownList)e.Row.FindControl("cmbsection");
 
            //if (cmbType != null)
            //{
            //    try
            //    {
            //        cmbType.DataSource = softwareRequirements.FetchSectionType();
            //        cmbType.DataBind();
            //        cmbType.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString();
            //    }
            //    catch (Exception err)
            //    {
            //        //TextBox1.Text = "Error reading list of names. ";
            //        //TextBox1.Text += err.Message;
            //    }
            //}
 
 
 
 
 
 
 
        }
 
        //if (e.Row.RowType == DataControlRowType.Footer)
        //{
        //    //DropDownList txtNewisTraced = (DropDownList)e.Row.FindControl("cmbNewsection");
        //    //txtNewisTraced.DataSource = softwareRequirements.FetchSectionType();
        //    //txtNewisTraced.DataBind();
        //}
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        FillSoftwareRequirementsInGrid();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        softwareRequirements.Delete(Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString()));
        FillSoftwareRequirementsInGrid();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        FillSoftwareRequirementsInGrid();
 
 
 
    }
 
    protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
    {
 
 
        GridViewRow row = GridView1.SelectedRow;
        string message = "You selected " + row.Cells[2].Text + ".";
 
 
    }
 
    protected void GridView1_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
    {
 
        // Get the currently selected row. Because the SelectedIndexChanging event
        // occurs before the select operation in the GridView control, the
        // SelectedRow property cannot be used. Instead, use the Rows collection
        // and the NewSelectedIndex property of the e argument passed to this 
        // event handler.
        GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
 
        // You can cancel the select operation by using the Cancel
        // property. For this example, if the user selects a customer with 
        // the ID "ANATR", the select operation is canceled and an error message
        // is displayed.
        if (row.Cells[1].Text == "ANATR")
        {
 
            e.Cancel = true;
            TextBox1.Text = "You cannot select " + row.Cells[2].Text + ".";
 
        }
 
    }
 
 
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox txtsoftwareRequirementId = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtsoftwareRequirementId");
        TextBox txtprojectId = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtprojectId");
        TextBox txtsection = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtsection");
        //DropDownList cmbsection = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("cmbsection");
        TextBox txtrowNumber = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtrowNumber");
        TextBox txtsoftwareRequirement = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtsoftwareRequirement");
        TextBox txtisTraced = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtisTraced");
        TextBox txtimage = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtimage");
 
        softwareRequirements.Update(Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString()), projectId, txtsection.Text, txtrowNumber.Text, txtsoftwareRequirement.Text, txtisTraced.Text, txtimage.Text);
        GridView1.EditIndex = -1;
        FillSoftwareRequirementsInGrid();
    }
 
 
    protected void Button1_Click(object sender, EventArgs e)
    {
 
        HtmlDocument doc = new HtmlDocument("1");
        doc.printDocument();
 
    }
}